PostgreSQL failover: a safe procedure for switching the primary
PostgreSQL failover promotes a standby to become the new writable primary when the current primary cannot serve the database. The promotion command is short; the safety work comes first: prevent the old primary from accepting writes, and assess how much WAL the candidate has replayed. Skipping either step can create two writable primaries or lose commits that had not reached the standby.
This guide follows PostgreSQL 19 documentation reviewed on 23 November 2026. It covers a manually directed failover of a physical streaming standby. PostgreSQL does not include HA management software that detects primary failure and notifies the standby; your environment must provide detection, fencing, and client routing. Managed-service procedures and HA-manager setup are out of scope.
Failover or planned switchover?
Failover responds to an unexpected primary failure. A planned switchover moves roles while both servers are healthy and can be checked. During failure, the primary's state and the standby's latest WAL may be uncertain. A reporting standby used only for read-only work does not need promotion. PostgreSQL 19: Failover
Define RTO (acceptable service interruption) and RPO (acceptable data loss) in advance. With asynchronous replication, a commit can be acknowledged before its WAL reaches the standby, so recent transactions may be missing after promotion. Synchronous replication can reduce that risk, but adds network latency and can reduce write availability when required standbys are unreachable. Check the configured synchronous standbys and commit mode; the word “synchronous” alone is not a universal guarantee. High Availability · Warm Standby
What must be checked before promotion?
Determine whether the primary process is down or only unreachable across a network path. In a partition, the old primary may still accept writes. PostgreSQL calls for a mechanism such as fencing/STONITH to ensure the old primary cannot continue as primary; a missing heartbeat or monitoring alert alone does not prove isolation. Plan and test a host, storage, or cluster-manager fencing method before an incident. Failover and STONITH
Choose the healthiest eligible standby and compare received and replayed WAL positions. During recovery, pg_last_wal_receive_lsn() reports the receive position and pg_last_wal_replay_lsn() the replay position. WAL received but not replayed is not yet reflected in database state. These observations inform the decision; they do not always calculate the exact business data loss. Record uncertainty if the former primary cannot be inspected. System Administration Functions
| Gate | Verify | If uncertain |
|---|---|---|
| Failure | Primary health, independent network paths, impact | Diagnose through another path before promotion |
| Fencing | Evidence old primary cannot accept writes | Do not promote yet |
| Candidate | Topology, replay position, candidate priority | Compare loss risk and record the choice |
| Service | Promotion, one writable endpoint, client routing | Keep traffic closed and investigate |
How do you promote and restore traffic?
Only after confirming the candidate and fencing the former primary, use the environment's approved procedure. This core PostgreSQL example is illustrative; confirm the host and data directory before running it:
pg_ctl promote -D /path/to/standby-dataAlternatively, call pg_promote(); with waiting enabled it waits for promotion or timeout and reports success as a Boolean. Check permissions. pg_ctl promote signals a standby to end standby mode and begin read/write operation. pg_ctl · pg_promote()
Treat command completion and service recovery as separate checks. Inspect logs and recovery state, test reads and a controlled write, then move the proxy, service discovery, virtual IP, or connection strings. Check that pooled application connections reach the new primary and that retries are not writing to the old address. Monitor errors, locks, backups, and replication. No promotion was run for this article.
How can the old primary rejoin?
After failover, the cluster may have only one server, a degraded state. Do not simply restart the old primary: it may follow a divergent timeline or accept writes. Keep it fenced and rebuild it as a standby of the new primary. pg_rewind can synchronize diverged copies by copying changed blocks, but requires prerequisites such as checksums or wal_log_hints, the needed WAL, and a cleanly stopped target. A failed rewind can leave the target unusable; PostgreSQL recommends taking a fresh backup. WAL replay and standby configuration are still needed afterward. pg_rewind
Keep the degraded-state risk visible until the rebuilt standby is streaming within your operating threshold. Rejoining and later switching roles are separate changes; verify that data flows in only one direction.
How should the procedure be rehearsed?
Document failure criteria, fencing evidence, candidate priority, command location, routing change, application checks, rebuild steps, owners, and communications. Rehearse role switching in an approved window with explicit stop and rollback conditions. Success means the old primary is fenced, only one server receives writes, the loss uncertainty is recorded, clients use the right endpoint, and backup/standby protection is restored—not merely that promotion returned successfully. PostgreSQL recommends written procedures and regular switching to test the mechanism. Failover operations
This article explains PostgreSQL 19 core behavior, not managed-provider or HA-manager commands. It does not describe or test an actual production failover. Detailed fencing design is a separate subject.
Sources
[PostgreSQL 19: High Availability](https://www.postgresql.org/docs/19/high-availability.html)
[PostgreSQL 19: Warm Standby](https://www.postgresql.org/docs/19/warm-standby.html)
[PostgreSQL 19: Failover](https://www.postgresql.org/docs/19/warm-standby-failover.html)
[PostgreSQL 19: System Administration Functions](https://www.postgresql.org/docs/19/functions-admin.html)
[PostgreSQL 19: pg_ctl](https://www.postgresql.org/docs/19/app-pg-ctl.html)
[PostgreSQL 19: pg_rewind](https://www.postgresql.org/docs/19/app-pgrewind.html)