If you’re running a PostgreSQL database on Cloud SQL and need a replica for backup or scale-out purposes, you can set up replication to a replica VM using pglogical. In this blog post, we’ll guide you through the necessary steps to set up replication between Cloud SQL with PostgreSQL 14 as your primary and a replica VM running Red Hat Enterprise Linux 7.
First, let’s start with the primary (Cloud SQL) side.
Edit Cloud SQL Flags
The first step is to edit the Cloud SQL flags. Use the following flags to enable pglogical and logical decoding:
Login to PostgreSQL
Once the flags are set, log in to PostgreSQL using the psql
command:
Create Replication User
Create a replication user with the following command, replacing <REPLICATION_USER>
and <REPLICATION_USER_PASSWORD>
with your preferred values:
Connect to the Database
Connect to the database you want to replicate using the replication user:
Enable pglogical Extension
Enable the pglogical extension with the following command:
Create a pglogical Node
Create a pglogical node with the following command, replacing <CLOUDSQL_IP>
, <DATABASE_NAME>
, <REPLICATION_USER>
, and <REPLICATION_USER_PASSWORD>
with your preferred values:
Add Tables to Replication Set
Add all tables to the replication set with the following command:
You can also add individual tables to the replication set with the following command:
Now, let’s move on to the replica VM side.
Install PostgreSQL 14 and pglogical
Install PostgreSQL 14 and pglogical on your Red Hat Enterprise Linux 7 replica VM by following the instructions provided by PostgreSQL and pglogical.
Install Postgresql 14 https://www.postgresql.org/download/linux/redhat/
Install pglogical https://github.com/2ndQuadrant/pglogical
Edit pg_hba.conf
Edit the /var/lib/pgsql/14/data/pg_hba.conf
file and add the following line:
Edit postgresql.conf
Edit the /var/lib/pgsql/14/data/postgresql.conf
file and set listen_addresses to ’*‘.
Restart PostgreSQL
Restart PostgreSQL with the following command:
Create Replication User
Create the same replication user on the replica VM as you did on the primary:
Set Parameters
Set the following parameters with the following commands:
Restart PostgreSQL
Restart PostgreSQL with the following command:
Login as <REPLICATION_USER>
Create a database
Create the same <DATABASE_NAME>
as the database you want to replicate in Cloud SQL and exit
Dump database table schema from Cloud SQL
Use the following command dump database table schema from Cloud SQL:
Restore table schema
Restore the table schema to the replica database with the following:
Login as <REPLICATION_USER>
Let’s login to the database you created previously as <REPLICATION_USER>
Enable pglogical extension
If you got an error, this means that the pglogical extension is already enabled
Create a pglogical node:
Create a pglogical subscription:
Check the status of the subscription:
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.
Notes:
Everytime you create a new table in the Cloud SQL (PRIMARY), you have to create the same table schema in REPLICA and add the table to replica set in the PRIMARY with the following command:
Additional commands:
Remove replication set
Remove pglogical subscription
Remove pglogical node
You can also check the complete documentation for pglogical here https://www.2ndquadrant.com/en/resources-old/pglogical/pglogical-docs/