Postgresql 13 Streaming Replicat
Postgresql 13 can be considered revolutionary considering the performance boost, configuration simplify, planner improvments and other enhancements such as SQL features, indexing improvments, etc.
About the streaming replication configuration, it's easier than the previews verisons which older than Postgresql 11(there is no recovery.conf file in standby anymore).
This post will describe the steps on PostgreSQL 13 Streaming Replication on CentOS 8.
Installing PostgreSQL 13
# Install the repository RPM:
sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm
# Disable the built-in PostgreSQL module:
sudo dnf -qy module disable postgresql
# Install PostgreSQL:
sudo dnf install -y postgresql13-server
# Optionally initialize the database and enable automatic start:
sudo /usr/pgsql-13/bin/postgresql-13-setup initdb sudo
systemctl enable postgresql-13 sudo
systemctl start postgresql-13
#change password
sudo passwd -d postgres
ALTER USER postgres WITH PASSWORD 'your_password_string';
\q
#check status
ps -ef | grep post
service postgresql-13 status
Master node setup
check the $PGDATA is set under the user 'postgres', if not, export it like following.
sudo su - postgres # check the PGDATA
# echo $PGDATA # cat .bash_profile
# /usr/lib/systemd/system/postgresql-13.service
# Environment=PGDATA=/var/lib/pgsql/13/data/
echo "export PATH=/usr/pgsql-13/bin:$PATH PAGER=less" >> ~/.pgsql_profile echo "export PGDATA=/var/lib/pgsql/13/data" >> ~/.pgsql_profile
# source ~/.pgsql_profile
Modify the parameter listen_addresses to allow a specific IP interface or all (using *).
#
psql -c "ALTER SYSTEM SET listen_addresses TO '*'";
# Modifying this parameter requires a restart of the PostgreSQL instance to get the change into effect.
sudo systemctl restart postgresql-13
Create replicator user in database
psql -c "CREATE USER replicator WITH REPLICATION ENCRYPTED PASSWORD 'the password for db_replicator user'";
Add slave IPs into master config, in my case I put last line to allow everyone to access the database.
echo "host replication replicator 192.168.3.138/32 scram-sha-256" >> $PGDATA/pg_hba.conf echo "host replication replicator 192.168.3.139/32 scram-sha-256" >> $PGDATA/pg_hba.conf echo "host all all 0.0.0.0/0 scram-sha-256" >> $PGDATA/pg_hba.conf
Reload it
# Reload the psql -c "select pg_reload_conf()"
# Firewall check
systemctl status firewalld.service
systemctl status iptables.service
systemctl status ip6tables.service
systemctl stop firewalld.service
systemctl disable firewalld.service
Slave Node setup
Stop the database
sudo systemctl stop postgresql-13
systemctl status postgresql-13
Remove the data directory, you can backup it if you like
# backup data
zip -r /var/lib/pgsql/13.zip /var/lib/pgsql/13
rm -rf $PGDATA/ pg_basebackup -h 192.168.3.137 -U replicator -p 5432 -D $PGDATA -Fp -Xs -P -Rsystemctl start postgresql-13
systemctl status postgresql-13
Review the slave node:
$PGDATA/standby.signal
This is an important file that must exist in a standby data directory to help postgres determine its state as a standby. It is automatically created when it uses the "-R" option while running pg_basebackup.
$PGDATA/postgresql.auto.conf
This is the configuration file that is read at the end when you start Postgres. So, if there is a parameter that has different values in postgresql.conf and postgresql.auto.conf files, the value set in the postgresql.auto.conf is considered by PostgreSQL, ALTER SYSTEM would automatically be written to postgresql.auto.conf file by postgres.
Verify the replication in the Master node
psql -x -c "select * from pg_stat_replication"