Configure external replicas Stay organized with collections Save and categorize content based on your preferences.
logical decoding
.For more information about replication, seeAbout replication in Cloud SQL.
Set up the external replica configuration
Before you begin
Before you start this task, you must have a Cloud SQL instance and anexternal PostgreSQL instance that meets therequirements for externalreplicas.
Configure the primary instance
- Go to theCloud SQL Instances page in the Google Cloud console.
- Enable access on the primary instance for the IP address of the external replica.
For information about enabling IP access, seeConfiguring access for IP connections.
- 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'sOverview page.
- Click the Cloud Shell icon
in the upper right corner.
- At the Cloud Shell prompt, use the built-in PostgreSQL client to connect to your primary instance:
gcloudsqlconnectPRIMARY_INSTANCE_NAME\--user=postgres
- Enter your root password. You should then see the postgres prompt.
- Create a PostgreSQL user with the
REPLICATION
attribute.CREATEUSERREPLICATION_USERWITHREPLICATIONINROLEcloudsqlsuperuserLOGINPASSWORD'REPLICATION_USER_PASSWORD';
- Install and configure the pglogical extension:
Edit the Cloud SQL instance to add and set the following flags:
cloudsql.enable_pglogical
cloudsql.logical_decoding
max_replication_slots
max_worker_processes
max_wal_senders
For more information about these flags, see thePostgreSQL resources page.
Restart the database, then login, change to the replication_user, create the
pglogical
extension:CREATEEXTENSIONpglogical;
- Create a pglogical node:
A pglogical _node_ represents a physical PostgreSQL instance, and stores connection details for that instance.
SELECTpglogical.create_node(node_name:='provider',dsn:='host=PRIMARY_PUBLIC_IP_ADDRESS port=5432 dbname=DATABASE_NAME user=REPLICATION_USER password=REPLICATION_USER_PASSWORD');
- If you are starting with a new database, create the same database and tables on both the primary and replica instances. For example:
CREATEDATABASEtest;\connecttest;CREATETABLEreplica_test(idSERIALPRIMARYKEY,datatext);INSERTINTOreplica_test(data)VALUES('apple'),('banana'),('cherry');CREATEEXTENSIONpglogical;
- 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.
- 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:
SELECTpglogical.replication_set_add_table('default','replica_test',true);
Configure the external replica
- Create a special user for replication and grant replication privileges:
CREATEUSERREPLICATION_USERWITHREPLICATIONSUPERUSERLOGINPASSWORD'REPLICATION_USER_PASSWORD';
- If you are starting with a new database, use theREPLICATION_USER to create the same database and tables on both the primary and replica instances. For example:
CREATEDATABASEtest;\connecttest;CREATETABLEreplica_test(idSERIALPRIMARYKEY,datatext);INSERTINTOreplica_test(data)VALUES('apple'),('banana'),('cherry');
- 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:gcloudstoragecpgs://BUCKET_NAME/DUMP_FILE_NAME.
- Import the file into your database.
psql --user=postgres --password <DUMP_FILE_NAME.
- Install
pglogical
according to your OS. For example, on Debian systems running PostgreSQL version 13,sudo apt-get install postgresql-13-pglogical
. - Login to the database as the replication_user and set the following parameters:
ALTERSYSTEMSETshared_preload_libraries='pglogical';ALTERSYSTEMSETmax_replication_slots=#;(where#isthesameasyousetontheprimary).ALTERSYSTEMSETmax_worker_processes=#;(where#isthesameasyousetontheprimary).#Logoutofthedatabaseandrestartit.Forexample,#
sudo/etc/init.d/postgresqlrestart
#Logbackinthedatabaseasthereplication_user.#Sincethepglogicalextensioniscreatedlocaltoeachdatabase,youneedto#executeCREATEEXTENSIONpglogical
ineachdatabaseyoucreate,soifyou#haven'talreadydonethat:CREATEEXTENSIONpglogical;Formoreinformationabouttheseflags,seethePostgreSQLresourcespage.
- Create a pglogical node:
SELECTpglogical.create_node(node_name:='subscriber',dsn:='host=REPLICA_PUBLIC_IP_ADDRESS port=5432 dbname=DATABASE_NAME user=REPLICATION_USER password=REPLICATION_USER_PASSWORD');
- Create a pglogical subscription:
SELECTpglogical.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');
- Check the status of the subscription:
SELECT*FROMpglogical.show_subscription_status('SUBSCRIPTION_NAME');
- If the status appears as
replicating
, then the setup is successful. - Insert some data into the primary and check the replica to make sure the data appears there as well.
Troubleshoot
SeeTroubleshooting pglogicalWhat's next
- Learn how tomanage replicas.
- Learn aboutrequirements and best practices for the external replica configuration.
- Learn more aboutPostgreSQL replication.
- Learn more aboutreplication configuration settings.
- Learn more aboutreplicating from an external server.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-07-14 UTC.