配置外部副本
本页面介绍了如何使用
pglogical 扩展程序和
logical decoding
配置复制到 Cloud SQL 外部副本的 Cloud SQL 实例。
如需详细了解复制,请参阅 Cloud SQL 中的复制简介。
设置外部副本配置
准备工作
在开始此任务之前,您必须有一个 Cloud SQL 实例和一个符合外部副本要求的外部 PostgreSQL 实例。
- 在 Trusted Cloud 控制台中前往“Cloud SQL 实例”页面。
- 为外部副本的 IP 地址启用对主实例的访问权限。
如需了解如何启用 IP 访问权限,请参阅为 IP 连接配置访问权限。
- 记录主实例的公共 IP 地址和公共传出 IP 地址以备后用。您可以在实例的概览页面上找到这些值。
- 点击右上角的 Cloud Shell 图标 (
)。
- 在 Cloud Shell 提示符处,使用内置的 PostgreSQL 客户端连接到您的主实例:
gcloud sql connect PRIMARY_INSTANCE_NAME \
--user=postgres
- 输入您的根密码。 您随后应该会看到 postgres 提示符。
- 使用
REPLICATION
特性创建 PostgreSQL 用户。CREATE USER REPLICATION_USER WITH REPLICATION IN ROLE cloudsqlsuperuser LOGIN PASSWORD 'REPLICATION_USER_PASSWORD';
- 安装并配置 pglogical 扩展程序:
修改 Cloud SQL 实例以添加和设置以下标志:
重启数据库,然后登录并更改为 Replication_user,然后创建 pglogical
扩展程序:
CREATE EXTENSION pglogical;
- 创建 pglogical 节点:
pglogical _node_ 表示物理 PostgreSQL 实例,并存储该实例的连接详细信息。
SELECT pglogical.create_node(
node_name := 'provider',
dsn := 'host=PRIMARY_PUBLIC_IP_ADDRESS port=5432 dbname=DATABASE_NAME user=REPLICATION_USER password=REPLICATION_USER_PASSWORD'
);
- 如果要开始使用新数据库,请在主实例和副本实例上创建相同的数据库和表。例如:
CREATE DATABASE test;
\connect test;
CREATE TABLE replica_test (id SERIAL PRIMARY KEY, data text);
INSERT INTO replica_test (data) VALUES ('apple'), ('banana'), ('cherry');
CREATE EXTENSION pglogical;
- 如果主实例上已有数据库,您必须在副本上创建该数据库。为此,请将主实例中的数据库导出到一个 Cloud Storage 存储桶,并将其导入副本。详细了解如何将 Cloud SQL 中的数据导出到 Cloud Storage 中的 SQL 转储文件。
-
为了支持将不同的数据集复制到不同的目标位置,pglogical 提供了复制集的概念。例如,如需将表添加到默认复制集,请执行以下操作:
SELECT pglogical.replication_set_add_table('default', 'replica_test', true);
- 创建一个特殊用户以用于实现复制功能并授予复制权限:
CREATE USER REPLICATION_USER WITH REPLICATION SUPERUSER LOGIN PASSWORD 'REPLICATION_USER_PASSWORD';
- 如果要开始使用新数据库,请使用 REPLICATION_USER 在主实例和副本实例上创建相同的数据库和表。例如:
CREATE DATABASE test;
\connect test;
CREATE TABLE replica_test (id SERIAL PRIMARY KEY, data text);
INSERT INTO replica_test (data) VALUES ('apple'), ('banana'), ('cherry');
- 如果您要使用从主实例导出的文件作为外部副本实例的种子,请从 Cloud Storage 下载导出的文件。如果外部副本位于 Compute Engine 实例上,您可以使用
gcloud storage
命令下载文件:gcloud storage cp gs://BUCKET_NAME/DUMP_FILE_NAME .
- 将文件导入您的数据库。
psql --user=postgres --password < DUMP_FILE_NAME.
- 根据您的操作系统安装
pglogical
。例如,在运行 PostgreSQL 版本 13 的 Debian 系统上,sudo apt-get install postgresql-13-pglogical
。
- 以 Replication_user 身份登录数据库并设置以下参数:
ALTER SYSTEM SET shared_preload_libraries = 'pglogical';
ALTER SYSTEM SET max_replication_slots = #; (where # is the same as you set on the primary).
ALTER SYSTEM SET max_worker_processes = #; (where # is the same as you set on the primary).
# Logout of the database and restart it. For example,
# sudo /etc/init.d/postgresql restart
# Log back in the database as the replication_user.
# Since the pglogical extension is created local to each database, you need to
# execute CREATE EXTENSION pglogical
in each database you create, so if you
# haven't already done that:
CREATE EXTENSION pglogical;
For more information about these flags, see the PostgreSQL resources page.
- 创建 pglogical 节点:
SELECT pglogical.create_node(
node_name := 'subscriber',
dsn := 'host=REPLICA_PUBLIC_IP_ADDRESS port=5432 dbname=DATABASE_NAME user=REPLICATION_USER password=REPLICATION_USER_PASSWORD'
);
- 创建 pglogical 订阅:
SELECT pglogical.create_subscription(
subscription_name := 'SUBSCRIPTION_NAME',
provider_dsn := 'host=PRIMARY_PUBLIC_IP_ADDRESS port=5432 dbname=DATABASE_NAME user=REPLICATION_USER password=REPLICATION_USER_PASSWORD'
);
- 查看订阅的状态:
SELECT * FROM pglogical.show_subscription_status('SUBSCRIPTION_NAME');
- 如果状态显示为
replicating
,则表示设置成功。
- 将一些数据插入主实例并检查副本,以确保数据也出现在主实例中。
问题排查
请参阅
排查 pglogical 问题后续步骤
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-08。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-08。"],[],[],null,["# Configure external replicas\n\n\u003cbr /\u003e\n\n[MySQL](/sql/docs/mysql/replication/configure-external-replica \"View this page for the MySQL database engine\") \\| PostgreSQL \\| [SQL Server](/sql/docs/sqlserver/replication/configure-external-replica \"View this page for the SQL Server database engine\")\n\n\u003cbr /\u003e\n\nThis page describes how to configure a Cloud SQL instance that replicates to a replica external to Cloud SQL using the [pglogical extension](/sql/docs/postgres/replication/configure-logical-replication) with `logical decoding`.\n\nFor more information about replication, see\n[About replication in Cloud SQL](/sql/docs/postgres/replication).\n\nSet up the external replica configuration\n-----------------------------------------\n\n### Before you begin\n\nBefore you start this task, you must have a Cloud SQL instance and an\nexternal PostgreSQL instance that meets the [requirements for external\nreplicas](/sql/docs/postgres/replication#external-read-replicas).\n\n### Configure the primary instance\n\n1. Go to the [Cloud SQL Instances page](https://console.cloud.google.com/sql/instances) in the Google Cloud console.\n2. Enable access on the primary instance for the IP address of the external replica. For information about enabling IP access, see\n [Configuring access for IP connections](/sql/docs/postgres/configure-ip).\n\n3. Record the public IP address and the public outgoing IP address of the primary instance for later use. You can find these values on the instance's **Overview** page.\n4. Click the Cloud Shell icon in the upper right corner.\n5. At the Cloud Shell prompt, use the built-in PostgreSQL client to connect to your primary instance: \n\n ```bash\n \n gcloud sql connect PRIMARY_INSTANCE_NAME \\\n --user=postgres\n \n \n ```\n6. Enter your root password. You should then see the postgres prompt.\n7. Create a PostgreSQL user with the `REPLICATION` attribute. \n\n CREATE USER \u003cvar translate=\"no\"\u003eREPLICATION_USER\u003c/var\u003e WITH REPLICATION IN ROLE cloudsqlsuperuser LOGIN PASSWORD '\u003cvar translate=\"no\"\u003eREPLICATION_USER_PASSWORD\u003c/var\u003e';\n \n8. Install and configure the pglogical extension: Edit the Cloud SQL instance to add and set the following flags:\n\n - `cloudsql.enable_pglogical`\n - `cloudsql.logical_decoding`\n - `max_replication_slots`\n - `max_worker_processes`\n - `max_wal_senders`\n - For more information about these flags, see the [PostgreSQL resources](/sql/docs/postgres/replication/configure-logical-replication#postgresql-resources) page.\n\n Restart the database, then login, change to the replication_user,\n create the `pglogical` extension: \n\n ```sql\n CREATE EXTENSION pglogical;\n \n ```\n9. Create a pglogical node:\n A pglogical _node_ represents a physical PostgreSQL instance, and stores\n connection details for that instance.\n\n ```sql\n SELECT pglogical.create_node(\n node_name := 'provider',\n dsn := 'host=\u003cvar translate=\"no\"\u003ePRIMARY_PUBLIC_IP_ADDRESS\u003c/var\u003e port=5432 dbname=\u003cvar translate=\"no\"\u003eDATABASE_NAME\u003c/var\u003e user=\u003cvar translate=\"no\"\u003eREPLICATION_USER\u003c/var\u003e password=\u003cvar translate=\"no\"\u003eREPLICATION_USER_PASSWORD\u003c/var\u003e'\n );\n \n ```\n10. If you are starting with a new database, create the same database and tables on both the primary and replica instances. For example: \n\n ```sql\n CREATE DATABASE test;\n\n \\connect test;\n\n CREATE TABLE replica_test (id SERIAL PRIMARY KEY, data text);\n INSERT INTO replica_test (data) VALUES ('apple'), ('banana'), ('cherry');\n\n CREATE EXTENSION pglogical;\n ```\n11. If you already have a database on the primary instance, you must create the same on the replica. To do this, export the database from the primary instance to a Cloud Storage bucket and import it into the replica. Learn more about [Exporting data from Cloud SQL to a SQL dump file in Cloud Storage](/sql/docs/postgres/import-export/exporting#cloud-sql).\n12. To support replicating different sets of data to different destinations, pglogical has the concept of a replication set. For example, to add a table to the default replication set: \n\n ```sql\n SELECT pglogical.replication_set_add_table('default', 'replica_test', true);\n \n ```\n\n### Configure the external replica\n\n1. Create a special user for replication and grant replication privileges: \n\n CREATE USER \u003cvar translate=\"no\"\u003eREPLICATION_USER\u003c/var\u003e WITH REPLICATION SUPERUSER LOGIN PASSWORD '\u003cvar translate=\"no\"\u003eREPLICATION_USER_PASSWORD\u003c/var\u003e';\n \n2. If you are starting with a new database, use the \u003cvar translate=\"no\"\u003eREPLICATION_USER\u003c/var\u003e to create the same database and tables on both the primary and replica instances. For example: \n\n ```sql\n CREATE DATABASE test;\n \\connect test;\n CREATE TABLE replica_test (id SERIAL PRIMARY KEY, data text);\n INSERT INTO replica_test (data) VALUES ('apple'), ('banana'), ('cherry');\n \n ```\n3. If you are seeding the external replica instance with a file you exported file from the primary instance, download the exported file from Cloud Storage. If your external replica is on a Compute Engine instance, you can download the file using the `gcloud storage` command: \n\n ```bash\n gcloud storage cp gs://BUCKET_NAME/DUMP_FILE_NAME .\n \n ```\n4. Import the file into your database. \n\n ```\n psql --user=postgres --password \u003c DUMP_FILE_NAME.\n ```\n5. Install `pglogical` according to your OS. For example, on Debian systems running PostgreSQL version 13, `sudo apt-get install postgresql-13-pglogical`.\n6. Login to the database as the replication_user and set the following parameters: \n\n ALTER SYSTEM SET shared_preload_libraries = 'pglogical';\n ALTER SYSTEM SET max_replication_slots = #; (where # is the same as you set on the primary).\n ALTER SYSTEM SET max_worker_processes = #; (where # is the same as you set on the primary).\n # Logout of the database and restart it. For example,\n # sudo /etc/init.d/postgresql restart\n # Log back in the database as the replication_user.\n # Since the pglogical extension is created local to each database, you need to\n # execute CREATE EXTENSION pglogical in each database you create, so if you\n # haven't already done that:\n CREATE EXTENSION pglogical;\n For more information about these flags, see the /sql/docs/postgres/replication/configure-logical-replication#postgresql-resources page.\n \n7. Create a pglogical node: \n\n ```sql\n SELECT pglogical.create_node(\n node_name := 'subscriber',\n dsn := 'host=\u003cvar translate=\"no\"\u003eREPLICA_PUBLIC_IP_ADDRESS\u003c/var\u003e port=5432 dbname=\u003cvar translate=\"no\"\u003eDATABASE_NAME\u003c/var\u003e user=\u003cvar translate=\"no\"\u003eREPLICATION_USER\u003c/var\u003e password=\u003cvar translate=\"no\"\u003eREPLICATION_USER_PASSWORD\u003c/var\u003e'\n );\n \n ```\n8. Create a pglogical subscription: \n\n ```sql\n SELECT pglogical.create_subscription(\n subscription_name := '\u003cvar translate=\"no\"\u003eSUBSCRIPTION_NAME\u003c/var\u003e',\n provider_dsn := 'host=\u003cvar translate=\"no\"\u003ePRIMARY_PUBLIC_IP_ADDRESS\u003c/var\u003e port=5432 dbname=\u003cvar translate=\"no\"\u003eDATABASE_NAME\u003c/var\u003e user=\u003cvar translate=\"no\"\u003eREPLICATION_USER\u003c/var\u003e password=\u003cvar translate=\"no\"\u003eREPLICATION_USER_PASSWORD\u003c/var\u003e'\n );\n \n ```\n9. Check the status of the subscription: \n\n ```sql\n SELECT * FROM pglogical.show_subscription_status('\u003cvar translate=\"no\"\u003eSUBSCRIPTION_NAME\u003c/var\u003e');\n \n ```\n10. If the status appears as `replicating`, then the setup is successful.\n11. Insert some data into the primary and check the replica to make sure the data appears there as well.\n\nTroubleshoot\n------------\n\nSee [Troubleshooting pglogical](/sql/docs/postgres/replication/configure-logical-replication#troubleshooting-pglogical)\n\nWhat's next\n-----------\n\n- Learn how to [manage replicas](/sql/docs/postgres/replication/manage-replicas).\n- Learn about [requirements and best practices for the external replica configuration](/sql/docs/postgres/replication#external-read-replicas).\n- Learn more about [PostgreSQL replication](https://www.postgresql.org/docs/current/static/logical-replication.html).\n- Learn more about [replication configuration settings](https://www.postgresql.org/docs/current/static/runtime-config-replication.html).\n- Learn more about [replicating from an external server](/sql/docs/postgres/replication/replication-from-external)."]]