Postgresql streaming replication primary to standby step
Postgresql streaming replication primary to standby step
PGMaster - 13.126.75.102
PGSlave - 65.0.130.124
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
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
su – postgres
cp - /var/lib/pgsql/14/data/ /var/lib/pgsql/14/data_old/
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
....................................
5. Notice that standby.signal is created and the connection settings are appended to postgresql.auto.conf.
ls -ltrh /var/lib/pgsql/14/data/
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.
2. Now, try to create object or database in slave(standby) server. It throws error, because slave(standby) is read-only
server.
5. Lets create a database in master server and verify its going to replicate to slave or not.
create database stream;
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 '*';
Thats all. We have successfully setup streaming replication in PostgreSQL step by step on Ubuntu.