This article provides a 5-step procedure to perform a manual role swap (switchover) between a Primary and Standby node in a VMware Postgres environment using native streaming replication and pg_rewind. This is specifically for environments where high-availability monitoring tools like pg_auto_failover are not in use.
Run this command on the current primary node (e.g., pg05) to check replication lag and ensure zero data loss:
psql -d <database_name> -c "SELECT client_addr, pg_wal_lsn_diff(pg_current_wal_lsn(), replay_lsn) AS replay_lag_bytes FROM pg_stat_replication;"Note: Proceed only when replay_lag_bytes is 0 or very low.
Disconnect application traffic and execute a clean shutdown on the old primary (e.g., pg05):
/opt/vmware/postgres/<version>/bin/pg_ctl stop -D /var/lib/pgsql/ha -m fastOn the standby node (e.g., pg06), trigger promotion:
/opt/vmware/postgres/<version>/bin/pg_ctl promote -D /var/lib/pgsql/haVerify the node is in read-write mode:
psql -d <database_name> -c "SELECT pg_is_in_recovery();"Output must return f (false).
On the old primary (e.g., pg05), use pg_rewind to align the timeline with the new primary (pg06) without a full re-clone:
Synchronize timeline:
/opt/vmware/postgres/<version>/bin/pg_rewind --target-pgdata /var/lib/pgsql/ha --source-server="host=pg06 port=5432 user=postgres dbname=<database_name>"Create standby signal file:
touch /var/lib/pgsql/ha/standby.signalUpdate configuration: Edit postgresql.auto.conf to point to the new primary:
primary_conninfo = 'host=pg06 port=5432 user=<replicator_user> sslmode=require'Start PostgreSQL:
/opt/vmware/postgres/<version>/bin/pg_ctl start -D /var/lib/pgsql/haOn New Standby (pg05): SELECT pg_is_in_recovery(); (Result: t)
On New Primary (pg06): SELECT client_addr, state FROM pg_stat_replication; (Result: pg05 IP with state streaming)
If pg_rewind fails, it may be due to the old primary not having been shut down cleanly or a lack of WAL data. Ensure wal_log_hints or data checksums were enabled during cluster initialization.