Monday , November 25 2024

How To Failover and Failback PostgreSQL Replication

You may want to read the following articles about replication before reading this article.

How To Install PostgreSQL Replication By using archive_command On Centos“,

How To Install PostgreSQL Replication With Replication Slots

If you want to failover replication you should add trigger file to recovery.conf on secondary server. We have mentioned this in previous articles.

To perform failover we will create a file on specified directory in trigger file.

Failover PostgreSQL Replication

On Secondary Server(it will be new primary)

Connect to postgresql and execute the below command

Then exit from postgresql and perform following steps:

Stop Postgresql On Primary Server(it will be new secondary)

Create trigger file on Secondary Server(it will be new primary) To perform Failover

With postgres user:

After creating necessary file, failover process have performed. You can control recovery.conf file to understand the process completed succesfuly. If recovery.conf file changed to recovery.done, it means that failover process completed succesfully.

Check replication status On New Primary Server

As you can see, the reverse replication did not occur automatically.

Create Reverse Replication Again After Failover in PostgreSQL

Go to old master(new secondary) to install replication again between new master and new secondary server.

On new Primary Server:

On New Secondary

Create a recovery.conf file on data directory with below content

It makes more sense to copy the recovery.done file in the new master from the new primary server to the new secondary with the following script. But when you copy, some of the above parameters will not be displayed. For example, recovery_target_timeline = ‘latest’

Add these missing parameters to the new recovery.conf file.

On new primary server with postgres

On New Secondary Server:

If you copy it instead of creating a new file, remember to change the application_name, host, and primary_slot_name in the recovery.conf file on the new secondary server.

Edit postgresql.conf or postgresql.auto.conf (if you use auto conf) on data directory and remove below lines.

Delete below lines:

On new Primary Server:

Then edit  pg_hba.conf and check the below line. It should be as follows

On new secondary:

Finally start postgresql on new secondary

Check PostgreSQL Replication Status on New Primary Server

Connect To PostgreSQL and run below statement.

If you have done everything in order, you will see a result like the following.

Loading

About dbtut

We are a team with over 10 years of database management and BI experience. Our Expertises: Oracle, SQL Server, PostgreSQL, MySQL, MongoDB, Elasticsearch, Kibana, Grafana.

Leave a Reply

Your email address will not be published. Required fields are marked *