0% found this document useful (0 votes)
38 views4 pages

Postgresql Streaming Replication Primary To Standby Step

Standby step
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views4 pages

Postgresql Streaming Replication Primary To Standby Step

Standby step
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

PostgreSQL Stream replication

PGMaster - 13.126.75.102

PostgreSQL Slave/Replica name and IP address:

PGSlave - 65.0.130.124

On Master and Slave servers, PostgreSQL 14 must be installed.

Step1: Configurations on master server

1. On master server, configure the IP address(es) listen to for connections from clients in postgresql.conf by removing #
in front of listen_address and give *. Which means listen connections from all.

• listen_addresses - *
• wal_level - Replica
• hot_Standby. – On

2. Now, connect to PostgreSQL on master server and create replica login.

CREATE USER replicator WITH REPLICATION ENCRYPTED PASSWORD 'admin@123';

3. Enter the following entry pg_hba.conf file which is located in /etc/postgresql/14/main on master node
host replication replicator 65.0.130.124/24 md5

4. Now, restart the PostgreSQL on Master server by using below command.

sudo systemctl restart postgresql-14


Step2: Configurations on slave(standby) server

1. We have to stop PostgreSQL on Slave server by using following command.

sudo systemctl stop postgresql-14

2. Now, switch to postgres user and take backup of main(data) directory.

su – postgres

cp - /var/lib/pgsql/14/data/ /var/lib/pgsql/14/data_old/

3. Now, remove the contents of main(data) directory on slave server.

rm -rf /var/lib/pgsql/14/data/*

4. Now, use basebackup to take the base backup with the right ownership with postgres(or any user with right
permissions).
pg_basebackup -h 13.126.75.102 -U replicator -p 5432 -D /var/lib/pgsql/14/data -Fp -Xs -P -R -C -S pgstandby

Then provide the password for user replicator created in master server.
pg_basebackup: initiating base backup, waiting for checkpoint to complete

....................................

pg_basebackup: syncing data to disk ...

pg_basebackup: base backup completed

5. Notice that standby.signal is created and the connection settings are appended to postgresql.auto.conf.

ls -ltrh /var/lib/pgsql/14/data/

6. postgresql.conf and there is a standby.signal file present in the data directory.

7. Now connect the master server, you should be able to see the replication slot called pgstandby when you open the
pg_replication_slots view as follows.

SELECT * FROM pg_replication_slots;


Step3. Test replication setup

1. Now start PostgreSQL on slave(standby) server.

sudo systemctl start postgresql-14

2. Now, try to create object or database in slave(standby) server. It throws error, because slave(standby) is read-only
server.

create database testdb;

3. We can check the status on standby using below command.

SELECT * FROM pg_stat_wal_receiver;


4. Now, verify the replication type synchronous or aynchronous using below command on master database server.
SELECT * FROM pg_stat_replication;

5. Lets create a database in master server and verify its going to replicate to slave or not.
create database stream;

6. Now, connect to slave and verify the database copied or not.


select datname from pg_database;

7. If you want to enable synchronous, the run the below command on master database server and reload postgresql
service.
ALTER SYSTEM SET synchronous_standby_names TO '*';

systemctl reload postgresql

Thats all. We have successfully setup streaming replication in PostgreSQL step by step on Ubuntu.

You might also like