Skip to content

RootDBA

DBA expertise: PostgreSQL, SQL Server & MySQL

Menu
  • Home
  • blog
  • PostgreSql
  • MySql
  • Sql Server
  • About AHMED – Root DBA
Menu

How to failover stream replication in PostgreSQl

Posted on September 1, 2025September 1, 2025 by AHMED

verify the replication on slave server

before we start the failover , we need to verified the syn between the master and slave

in the slave run the below command and if its return 0 means no delay 

SELECT CASE WHEN pg_last_wal_receive_lsn() = pg_last_wal_replay_lsn() THEN 0 ELSE EXTRACT(EPOCH FROM now() – pg_last_xact_replay_timestamp()) END AS log_delay;

stop the primary server

once you confirmed everything is fine , you need to stop the services either by shutdown the server or stop PostgreSQL services
for this purpose i will stop the PostgreSQL services .
systemctl stop postgresql

promote the standby to read-write

next step is to promote the standby server to be able to read and write
using the following command 

psql -c “SELECT pg_promote();” #This command promotes the standby to a read-write primary.

no if you try to create database it will allow since we enable read-write
efc715a5d9455f9fed72b62eefb34363.png

edit the pg_hba.conf

edit the pg_hba.conf in slave server and add the ip of the master server to act as slave
cd /etc/postgresql/12/main echo "host replication replication 10.10.10.80/24 md5" >> pg_hba.conf

Create standby.signal on Old Primary

Create the standby.signal file on the old primary to ensure it starts as a standby when brought back online:

touch /etc/postgresql/12/main/standby.signal

also edit the below postgresql.auto.conf on master server
with the below paramter 

#Update the postgres.auto.conf file with NEW MASTER SERVER DETAILS vi /etc/postgresql/12/main/postgresql.auto.conf #Modify the primary_conninfo parameter to reflect the new standby IP: primary_conninfo = ‘user=replication password=123456789 host=10.10.10.81 port=5432 sslmode=prefer sslcompression=0 gssencmode=prefer krbsrvname=postgres target_session_attrs=any’

after that start the PostgreSQL services on master server 

systemctl start postgresql

check if old master id read only

run the below command if its return T means the database is in read only if F means the database in read write 

SELECT pg_is_in_recovery(); This query will return a single boolean value: true if the server is a standby and false if it’s the primary

failback

fist verify that database is syncing by running the below command on slave server and should be 0

SELECT CASE WHEN pg_last_wal_receive_lsn() = pg_last_wal_replay_lsn() THEN 0 ELSE EXTRACT(EPOCH FROM now() – pg_last_xact_replay_timestamp()) END AS log_delay;

master failback config part

start by stopping the services on master server 

systemctl stop postgresql

make sure the below parameter is added in master pg_hba.conf
host replication replicator 10.10.10.80/24 md5″

cd /etc/postgresql/12/main/ nano pg_hba.conf

promote the slave (old-master)

go to the slave server (old master ) and make it read-write ,
using the below command 

psql -c “SELECT pg_promote();”

create single file on master server (old slave )

create the singe file on salve (old master)

touch /var/lib/postgresql/12/main/standby.signal

Edit postgresql.auto.conf on Old Primary

cd /etc/postgresql/12/main nano postgresql.auto.conf

add the below line ensure to mention the slave (old-master) ip

primary_conninfo = ‘user=replication password=123456789 host=10.10.10.80 port=5432 sslmode=prefer sslcompression=0 gssencmode=prefer krbsrvname=postgres target_session_attrs=any’

start the master (old slave ) services

start the the services using the below command 

systemctl start postgresql

check the log and observer if the server started as standby
`
cd /var/log/postgresql/
nano postgresql-12-main.log

`
7dbe95c5e329c6598ebe284fe27f880f.png

0
Visited 12 times, 1 visit(s) today
Category: PostgreSql, Postgresql Dba Guide

Post navigation

← PostgreSql master-slave replication
PostgreSql logical replication →
© 2025 RootDBA | Powered by Minimalist Blog WordPress Theme