Home » How to Configure Streaming Replication on PostgreSQL 9.5 Instances

How to Configure Streaming Replication on PostgreSQL 9.5 Instances

by tuanlp

 In this article, I will show you Postgresql installation and its streaming replication configuration of two PostgreSQL instances on different hosts. Our goal is to install Postgres 9.5 on master host and Postgres 9.5 on slave host, and configure streaming replication between them.

Installation and Configuration

To start with, we will install and prepare PostgreSQL 9.5 on master host running CentOS. First of all, we need to to use rpm command to do query for centos-release package to reveal CentOS version:

rpm --query centos-release

In our example we will consider CentOS release 6.8 with x86_64 architecture. Now, we need to install rpm package in order to run yum install command:

rpm -i <https://yum.postgresql.org/9.5/redhat/rhel-6.8-x86_64/pgdg-redhat95-9.5-3.noarch.rpm>

Please check that everything is on the right place by running:

yum list postgresql95*

Now you can install PostgreSQL 9.5:

yum install postgresql95-server.x86_64

Other packages can be installed according to your needs.

Once Postgresql-9.5 server is installed, a database needs to be initialized and configured. The first command (only needed once) is to initialize the database in PGDATA.

/etc/init.d/postgresql-9.5 initdb

or

service postgresql-9.5 initdb

If you want PostgreSQL to start automatically when the OS start, do the following:

chkconfig postgresql-9.5 on

At this moment you are ready to start the service, but before that we will update /var/lib/pgsql/9.5/data/postgresql.conf file. Please uncomment listen_addresses line and type in your IP address together with localhost.

listen_addresses = 'your IP, 127.0.0.1'

Then, please modify /var/lib/pgsql/9.5/data/pg_hba.conf file as following:

# IPv4 local connections:
host all all 127.0.0.1/32 md5
host all allyour IP md5

To control the database service, use

service postgresql-9.5 [command]

where [command] can be:

start – start the databasestop – stop the databaserestart – stop/start the database. Usually used to read changes to core configuration files.reload – reload pg_hba.conf file while keeping database running

Please start the service and we are done with Postgresql 9.5 installation on master host.

Next step, we will install Postgresql 9.5 on slave host. Steps are pretty much the same. To start with, we need to install rpm package.

Reminder: following steps should be performed on another host, so-called slave host.

rpm -i <https://yum.postgresql.org/9.5/redhat/rhel-6.8-x86_64/pgdg-redhat95-9.5-3.noarch.rpm>

and install Postgresql 9.5

yum install postgresql95-server.x86_64

Then initialize it

service postgresql-9.5 initdb

Make it start automatically on startups

chkconfig postgresql-9.5 on

And modify configuration file /var/lib/pgsql/9.5/data/postgresql.conf as following:

listen_addresses = 'your IP, 127.0.0.1'

Next, update /var/lib/pgsql/9.5/data/pg_hba.conf file:

# IPv4 local connections:
host all all 127.0.0.1/32 md5
host all allyour IP md5

Now you are ready to start the service

service postgresql-9.5 start

Replication Configuration

Thực hiện trên Server Master

Đưa PostgreSQL database về archive mode

Kiểm tra database đã ở chế độ archive mode hay chưa

# show archive_mode ;
 archive_mode 
--------------
 on
(1 row)

Nếu chưa, bạn hãy đưa database về chế độ Archive mode theo hướng dẫn dưới đây

Tạo thư mục lưu archive

Chúng ta cần tạo thư mục để lưu archive và chú ý thư mục đó phải cho phép user postgres có thể đọc ghi vào

# mkdir -p /backup/archive/
# chown postgres:postgres /backup/archive/

Chỉnh sửa tham số archive_mode và archive_command;

alter system set archive_mode = on;
alter system set archive_command = 'test ! -f /backup/archive/%f && cp %p /backup/archive/%f';

Restart PostgreSQL

$ pg_ctl restart

Tham số wal_level=replica

Kiểm tra wal_level

Để kích hoạt được archive log mode, yêu cầu bắt buộc wal_level phải từ replica trở lên.

postgres=# show wal_level ;
 wal_level 
-----------
 replica
(1 row)

Như vậy, wal_level đã thỏa mãn rồi

Nếu chưa, bạn hãy chạy lệnh sau để thay đổi tham số :

alter system set wal_level=replica;
Nếu bản 9.5 thì là
alter system set wal_level=hot_stanby;

Và nhớ restart lại instance để giá trị mới có hiệu lực.

pg_ctl restart

Cấu hình network giữa primary và standby

Tạo user để đồng bộ

CREATE USER replication WITH REPLICATION PASSWORD 'Abcd1234';

Sau đó, bạn sửa lại file pg_hba.conf để cho phép từ standby có thể kết nối được vào primary thông qua user replication

postgres=# show hba_file ;
              hba_file              
------------------------------------
 /var/lib/pgsql/12/data/pg_hba.conf
(1 row)

vi /var/lib/pgsql/12/data/pg_hba.conf

và thêm vào dòng sau:

host  replication     replication     [MASTER IP 192.168.50.35/32]         md5

Sau khi chỉnh xong file pg_hba.conf, bạn nhớ chạy lệnh sau để thay đổi có hiệu lực

pg_ctl reload

Cấu hình tham số

$ psql
psql (13.2)
Type "help" for help.
postgres=# 
postgres=# show config_file;
              config_file               
----------------------------------------
 /var/lib/pgsql/13/data/postgresql.conf
vi /var/lib/pgsql/13/data/postgresql.conf
# Add settings for extensions here
max_connections = 200
shared_buffers = 15GB
effective_cache_size = 45GB
work_mem = 39321kB
maintenance_work_mem = 2GB
checkpoint_segments = 32
checkpoint_completion_target = 0.9
wal_buffers = 16MB
default_statistics_target = 100

#logging settings
log_directory = '/var/log/pg_log95'
log_filename = 'postgresql-%d_%H%M%S.log'
log_truncate_on_rotation = on
log_rotation_age = 1d
log_rotation_size = 0
log_min_duration_statement = 30000
log_connections = true
tcp_keepalives_idle = 30

# Streaming replication
# To enable read-only queries on a standby server, wal_level must be set to
# "hot_standby". But you can choose "archive" if you never connect to the
# server in standby mode.
wal_level = hot_standby

# Set the maximum number of concurrent connections from the standby servers.
max_wal_senders = 5

# Enable WAL archiving on the primary to an archive directory accessible from
# the standby. If wal_keep_segments is a high enough number to retain the WAL
# segments required for the standby server, this is not necessary.
archive_mode = on

Thực hiện trên Server Slave

Như vậy các bước trên master đã xong. Bây giờ chúng ta chuyển sang thao tác trên slave.

We need to move the data directory on the slave host

# mv /opt/PostgreSQL/9.5/data /opt/PostgreSQL/9.5/data_bkp
# mkdir -p /opt/PostgreSQL/9.5/data
# chown postgres:postgres /backup/archive/
# chmod 700 /opt/PostgreSQL/9.5/data

Now, you need to make a base backup by copying the master hosts data directory to the slave host. You can do it with pg_basebackup command on slave host.

pg_basebackup -h 200.0.0.8 -U replication -p 5432 -D /opt/PostgreSQL/9.5/data -Fp -Xs -P -R

(Nhớ điền password của user replication)

Câu lệnh trên sẽ kết nối vào master, backup toàn bộ thư mục PGDATA của master và restore sang slave. Option -R sẽ tạo ra trên slave 1 file có tên là standby.signal. File này có nhiệm vụ báo hiệu đây là database slave.

Sau khi restore xong, kết quả có thể trông như sau:

Ngoài ra, nếu bạn kiểm tra file postgresql.auto.conf sẽ thấy có thêm tham số sau (tham số này cũng do option -R tạo ra)

primary_conninfo = 'user=replication password=''Abcd1234'' channel_binding=prefer host=192.168.50.34 port=5432 sslmode=prefer sslcompression=0 ssl_min_protocol_version=TLSv1.2 gssencmode=prefer krbsrvname=postgres target_session_attrs=any'

Tham số này để chỉ ra thông tin kết nối đến master, từ đó slave có thể sử dụng để kết nối vào slave để kéo WAL file về.

Start database

Sau khi đã restore xong, các bạn hãy start database lên

pg_ctl start

waiting **for** server to start....2021-*06*-11 15:52:36.115 +*07* [17767] LOG: redirecting log output to logging collector process

2021-*06*-11 15:52:36.115 +*07* [17767] HINT: Future log output will appear **in** directory "log".

done

server started

Như vậy là xong rồi đấy. Rất đơn giản phải không?


Kiểm tra kết quả

Bây giờ hãy thử kiểm tra kết quả xem

SELECT client_addr, state FROM pg_stat_replication;

Running this query on the primary cluster will output something similar to the following:

Output
   client_addr    |  state
------------------+-----------
 your_replica_IP | streaming

Bạn xem log của database xem.

cd $PGDATA/pg_log

tail -100f postgresql-Fri.log

Kết quả như sau:

!

Đoạn log trên cho thấy database hiện đang ở chế độ standby và cho phép các kết nối read-only (chỉ đọc).

Cách tiếp theo để kiểm tra đó là: Bạn thêm dữ liệu vào bên master và kiểm tra slave xem đã có dữ liệu vừa mới thêm hay chưa.

Cài trên ubuntu 20.04 làm theo hướng dẫn sau

https://www.digitalocean.com/community/tutorials/how-to-set-up-physical-streaming-replication-with-postgresql-12-on-ubuntu-20-04

You may also like