PostgreSQL – Preparing a Former Master to Run as Slave Again

postgresqlreplication

I have a streaming replication setup using postgresql 9.3. I have two servers, the master (server_1) and the slave (server_2).

The other day I did a failover to my slave (server_2). Now server_2 has been running as master for a couple of days, and I have now setup my primary master (server_1) again, and it now runs as master. I would like to avoid reinstalling server_2 before turning it into a slave of server_1 again. How can I do that? What dir's should I delete etc, in order to have a "clean" server that I can use as slave again?

Best Answer

The only officially supported method is to make a new base backup, or:

  • pg_start_backup() on the new master
  • rsync from new master to old master
  • pg_stop_backup()
  • ensure WAL got copied over and applied
  • create recovery.conf
  • start resynced server

The 3rd party tool pg_rewind is intended for that purpose. I haven't worked with it as yet and can't vouch for it in production.