I will tell how to install PostgreSQL Replication by using archive_command parameter on centos in this article. But usually I prefer to install postgresql replication with slot method. To learn more about installing replication with slot method, read the below article.
“How To Install PostgreSQL Replication With Slots”,
“How To Failover and Failback PostgreSQL Replication”
You may want to read the below article to install PostgreSQL on Centos.
Install PostgreSQL Replication by using archive_command
Switch to the postgres user with the command below and connect to postgres using the psql command.
su - postgres
Create a user named replicauser which will be connected to the master server for replica with the help of the following script.
create user replicauser with replication encrypted password 'rp';
Edit pg_hba file on the master server to backup from the master server using the replicauser user on the standby server with the below command.
Add the below line to pg_hba.conf. This will allow the standby server to connect to the master server for replication.
192.168.1.29 -> Standby IP
host replication replicauser 192.168.1.29/32 trust
We also add a line to each server that contains the IP of the other server as follows so that the two servers can connect to each other. This line means that the related IP can connect to me with the specified user. I allow it.
host all all 192.168.1.29/32 trust
Then run the following command while connected with the Postgres user so that the changes take effect.
/usr/pgsql-10/bin/pg_ctl reload -D /postgres/pg_data
You can also do this with the following command while connected with root.
systemctl reload postgresql-10.service
In the next step, we use the following command to set some parameters in the postgresql.conf file.
First, we set the value of listen_adress * as follows, and remove the leading # sign.
listen_addresses = '*'
Our other settings are as follows;
archive_mode = on
archive_command = '/postgres/waltransport.sh %p %f'
Then we use the following command to create waltransport.sh file.
Below you can see the contents of the waltransport.sh file.
The IP below is the IP of the standby server.
rsync -ave ssh $1 firstname.lastname@example.org:/postgres/xlogarchive/$2
We make the owner of the waltransport file postgres user and grant necessary rights(700) by using chown and chmod.
chown postgres:postgres /postgres/waltransport.sh
chmod 700 /postgres/waltransport.sh
To create the standby, go to the standby server first and download the repo and install postgresql without initdb.
Then, using the script below, we back up the master postgresql over the standby server.
The following IP is the IP of the master server.
On Standby Server:
/usr/pgsql-10/bin/pg_basebackup -D /postgres/pg_standby -c fast -P -Fp -h 192.168.1.28 -p 5432 -U replicauser
Then go to the “/postgres/pg_standby” path on the standby server and create the recovery.conf file. The contents of the recovery.conf shoul be as follows.
The following IP is the IP of the standby server.
primary_conninfo='host=192.168.1.29 port=5432 user=replicauser password=rp'
restore_command='cp /postgres/xlogarchive/ %f %p'
archive_cleanup_command='/usr/pgsql-10/bin/pg_archivecleanup /postgre/xlogarchive %r'
Then start the standby service by showing the pg_standby folder and you will see that the replication starts without errors. Congratulations.