Postgres Replication
Postgres Replication
cd /usr/pgsql-9.6/bin
./postgresql96-setup initdb
Next, start postgres service and enable it to start automatically at system boot.
netstat -plntu
If you do not have a netstat command, install net-tools. It's part from the net-tools.
So PostgreSQL 9.6 has been started. But we still need to configure the password for
postgres user. Login as 'postgres' user, and then access the postgres 'psql' shell.
su - postgres
psql
Give the 'postgres' user new password with the query below.
\password postgres
Enter new password:
So PostgreSQL 9.6 has been started and a new password for postgres user has been
configured.
Step 3 - Configure Firewalld
Firewalld is default a firewall management tool on CentOS 7. We will start this service
and open the port for PostgreSQL connection.
Start firewalld and enable it to start automatically at system boot using the following
commands:
Next, add new postgres service to firewalld with the following commands.
firewall-cmd --list-all
And you will see the PostgreSQL service has been added to the firewalld.
NOTE: Run the Step 1, Step 2 and Step 3 on all Master and Slaves.
Step 4 - Configure Master server
In this step, we will configure a master server for the replication. This is the main
server, allowing read and write process from applications running on it. PostgreSQL on
the master runs only on the '10.0.15.10' IP address, and performs streaming replication
to the slave server.
Go to the pgsql data directory '/var/lib/pgsql/9.6/data' and edit the configuration
file 'postgresql.conf'.
cd /var/lib/pgsql/9.6/data
vim postgresql.conf
Uncomment the 'listen_addresses' line and change value of the server IP address to
'10.0.15.10'.
listen_addresses = '10.0.15.10'
For the synchronization level, we will use local sync. Uncomment and change value line
as below.
synchronous_commit = local
Enable archiving mode and give the archive_command variable a command as value.
archive_mode = on
archive_command = 'cp %p /var/lib/pgsql/9.6/archive/%f'
For the 'Replication' settings, uncomment the 'wal_sender' line and change value to 2
(in this tutorial, we use only 2 servers master and slave), and for the
'wal_keep_segments' value is 10.
max_wal_senders = 2
wal_keep_segments = 10
mkdir -p /var/lib/pgsql/9.6/archive/
chmod 700 /var/lib/pgsql/9.6/archive/
chown -R postgres:postgres /var/lib/pgsql/9.6/archive/
vim pg_hba.conf
Save and exit. All configuration is complete. Now, restart PostgreSQL 9.6 using the
following command.
Next, we need to create a new user with replication privileges. We will create a new
user named 'replica'.
Login as postgres user, and create a new 'replica' user with password 'aqwe123@'.
su - postgres
createuser --replication -P replica
Enter New Password:
So with this, PostgreSQL 9.6 Master configuration has been completed, and the user for
replication is created.
Step 5 - Configure Slave server
In this step, we will configure the Slave server. We want to replace postgres data
directory on the slave server with the postgres data from the master server, and then
configure slave server to run under the IP address '10.0.15.11', and finally enable
hot_standby on it for allowing only read without write.
Before we start to configure the slave server, stop the postgres service using the
systemctl command below.
cd /var/lib/pgsql/9.6/
mv data data-backup
Create new data directory and change the ownership permissions of the directory to the
postgres user.
mkdir -p data/
chmod 700 data/
chown -R postgres:postgres data/
Next, login as the postgres user and copy all data directory from the 'Master' server to
the 'Slave' server as replica user.
su - postgres
pg_basebackup -h 10.0.15.10 -U replica -D /var/lib/pgsql/9.6/data -P --xlog
Password:
Type your password and wait for data transfer from the master to the slave server.
After the transfer is complete, go to the postgres data directory and edit postgresql.conf
file on the slave server.
cd /var/lib/pgsql/9.6/data/
vim postgresql.conf
Enable 'hot_standby' on the slave server by uncommenting the following line and
change the value to 'on'.
hot_standby = on
vim recovery.conf
netstat -plntu
Step 6 - Testing
Installation and configuration for PostgreSQL 9.6 Master-Slave replication are complete.
To test the setup, check the state stream of the replication and test for data replication
from the Master to the Slave.
Log in to the Master server and switch to the postgres user.
su - postgres
Next, check the streaming state replication of PostgreSQL with the following
commands.
You should see the state value is 'streaming', and the sync_state is 'sync'.
Next, test by inserting data from the master and then check all data on the slave
server.
Log in as the postgres user and access the PostgreSQL shell on the 'MASTER' server.
su - postgres
psql
Create new table 'replica_test' and insert some data into it with the following insert
queries.
su - postgres
psql
Check all data from the table 'replica_test' using the query below.