How do I Re-Attach a Master once a Replica has been Promoted in Postgres

To reattach a master server once a replica has been promoted in PostgreSQL, you need to perform the following steps:

  1. On the new master server, edit the postgresql.conf configuration file and set the wal_level parameter to hot_standby. This will enable the new master server to keep a sufficient amount of transaction log data (WAL) to allow the old master server to connect and apply the changes.
  2. Restart the new master server for the changes to take effect.
  3. On the old master server, edit the postgresql.conf configuration file and set the hot_standby parameter to on. This will enable the old master server to connect to the new master server and start receiving the changes.
  4. In the recovery.conf file on the old master server, add the following lines to specify the connection details for the new master server:
standby_mode = 'on'
primary_conninfo = 'host=new_master_server_hostname port=5432 user=replication_user password=replication_password'

Replace new_master_server_hostname, replication_user, and replication_password with the actual hostname, username, and password of the new master server and the user that will be used for replication.

  1. Restart the old master server for the changes to take effect. The old master server should now be able to connect to the new master server and start receiving the changes.
  2. Once the old master server has caught up with the new master server, you can reconfigure the other replicas to connect to the old master server and start replicating from it.

Note that these are the basic steps for reattaching a master server once a replica has been promoted in PostgreSQL. There are many other factors to consider and configure, such as security, performance, and monitoring, to ensure that your replication setup is reliable and efficient. It is recommended to consult the PostgreSQL documentation and other resources to learn more about the details and best practices for reattaching a master server in PostgreSQL.

Leave a Comment