Skip to content

PostgreSQL streaming-replication manual promotion runbook

When Patroni isn't in the picture or automation is unavailable, you still need to promote a standby cleanly. Exact pg_ctl, recovery, and verification flow for a manual primary promotion.

You inherit a PostgreSQL deployment that runs without Patroni or any orchestrator — just a primary, a streaming replica, and a documented expectation that someone will manually promote when the primary dies. The primary dies at 4 a.m. on a Saturday. You SSH into the standby. What you type next determines whether your application is back in five minutes or whether you spend the rest of the weekend explaining why the standby came up two timelines behind the application’s last write. This is the runbook.

What’s happening

Manual promotion of a PostgreSQL streaming replica is a sequence of three operations that have to happen in the right order:

  • Confirm the primary is actually dead, not just unreachable. A replica promoted while the primary is still alive becomes a second primary writing to the same logical schema. Reconciling that afterwards is data loss for one side.
  • Drain in-flight WAL. A replica might be a few seconds behind the primary at the moment of failure. If you promote before replaying that WAL, the new primary starts a fresh timeline at the older position. Anything in the gap is lost.
  • Promote and verify. pg_ctl promote on the replica creates a recovery.signal-removal and a timeline switch. The replica goes from in archive recovery to accepting connections (read-write). Then you update connection strings, point pgBouncer/HAProxy at it, and restart application workers whose pools still cache the dead primary’s address.

PostgreSQL 12+ no longer uses recovery.conf — replication config lives in postgresql.auto.conf or postgresql.conf and a standby.signal file marks a server as standby. The promotion runbook below targets 14+, which is what the supported corpus is. If you’re on 11 or 10, replace standby.signal with recovery.conf and restore_command/primary_conninfo directives.

How to verify

Before promoting, capture three things from the surviving replica:

# 1. Confirm we are on a standby, not somehow already a primary
sudo -u postgres psql -c "SELECT pg_is_in_recovery();"
# Expected: t

# 2. Last received and last applied LSNs
sudo -u postgres psql -c "
  SELECT pg_last_wal_receive_lsn() AS received,
         pg_last_wal_replay_lsn()  AS replayed,
         pg_wal_lsn_diff(pg_last_wal_receive_lsn(), pg_last_wal_replay_lsn()) AS lag_bytes;"

# 3. Replication state — was streaming connected to the dead primary
sudo -u postgres psql -c "
  SELECT status, sender_host, sender_port, last_msg_send_time, last_msg_receipt_time
  FROM pg_stat_wal_receiver;"

What to look for:

  • pg_is_in_recovery() = t confirms this is still a standby. If f, somebody already promoted — stop and reassess.
  • received should equal or be very close to replayed. A nonzero lag_bytes means there’s WAL the replica has but hasn’t replayed yet. Wait for it to catch up before promoting.
  • last_msg_receipt_time shows when the replica last heard from the primary. If it’s recent (under 5 seconds), the primary may still be alive — check before promoting.

Now confirm the primary is actually dead, not partitioned:

# From the standby, try to reach the primary
ping -c 3 <primary-ip>
nc -zv <primary-ip> 5432

# From a third location (jump host, monitoring server), try the same
ssh jump.internal "nc -zv <primary-ip> 5432"

# If you have IPMI/iLO/cloud-console access, check power state from there

A primary that pings but doesn’t accept Postgres connections is usually dead. A primary that doesn’t ping but a third host can reach is a network partition between you and the primary — promoting from your side will create the two-primaries scenario you want to avoid. Use a different observation point when in doubt.

If you have any fencing mechanism (cloud API to power-off the VM, IPMI to send a hard reset, network ACL to block the dead primary’s port 5432 across the fleet) — use it before promoting. STONITH-style fencing is what makes manual promotion safe.

The fix

This runbook assumes streaming replication with wal_level = replica, hot_standby = on, and a working restore_command for any gap-fill from archive. PostgreSQL 14+ syntax.

Step 1 — Fence the dead primary

If you have cloud-API access:

# Example: stop the VM via cloud provider CLI
# AWS:    aws ec2 stop-instances --instance-ids i-0abc...
# GCP:    gcloud compute instances stop pg01 --zone us-east1-b
# OpenStack: openstack server stop pg01

If you don’t have power control, at minimum block 5432 from the network so a zombie primary can’t accept writes:

# On every other host that has the primary in its connection string
sudo iptables -A OUTPUT -d <primary-ip> -p tcp --dport 5432 -j DROP

This is a fence, not a fix — undo it once the dead primary has been rebuilt or scrubbed.

Step 2 — Drain remaining WAL on the standby

If the standby is still receiving WAL via restore_command from an archive (object storage), wait for it to catch up:

# Watch the LSNs converge
sudo -u postgres psql -c "
  SELECT pg_last_wal_receive_lsn() AS received,
         pg_last_wal_replay_lsn()  AS replayed;"

When received == replayed and remains stable for 10-15 seconds, you have caught up to whatever the archive holds. There may still be a gap (WAL the primary committed but hadn’t archived yet) — that gap is your acceptable data loss for manual failover. It’s why managed services with synchronous replication or Patroni’s maximum_lag_on_failover exist.

If your replica was streaming directly from the dead primary with no archive, what’s there is what you have. Move on.

Step 3 — Promote the standby

# Promote using pg_ctl (recommended)
sudo -u postgres /usr/pgsql-16/bin/pg_ctl promote \
  -D /var/lib/pgsql/16/data

# OR using SQL (PG 12+)
sudo -u postgres psql -c "SELECT pg_promote(true, 60);"
# t = wait for promotion to complete, 60 = timeout in seconds

Behind the scenes, pg_ctl promote does:

  • Creates a promote.signal file in the data directory
  • The startup process notices it on the next WAL replay loop
  • The server exits archive recovery, finishes replaying any remaining WAL, picks the next timeline ID, writes a timeline-history file, and starts accepting writes

Verify:

sudo -u postgres psql -c "SELECT pg_is_in_recovery();"
# Expected: f

# What timeline are we on?
sudo -u postgres psql -c "SELECT pg_walfile_name(pg_current_wal_lsn());"
# Expected: something like 000000020000000B000000E4
#           ^^^^^^^^ the timeline ID has incremented (was 00000001, now 00000002)

If pg_is_in_recovery() still returns t, the promote didn’t take. Check the Postgres log for the reason — most often a stuck WAL replay or a missing archive file:

sudo tail -50 /var/lib/pgsql/16/data/log/postgresql-*.log

Step 4 — Update connection routing

The application doesn’t know the standby is the new primary until you tell it.

If you use HAProxy:

# Update the config and reload
sudo vim /etc/haproxy/haproxy.cfg
# In the postgres_write backend, mark the dead primary as 'disabled' or remove it
sudo systemctl reload haproxy
echo "show stat" | sudo socat stdio /run/haproxy/admin.sock \
  | awk -F, '$1=="postgres_write"'

If you use pgBouncer:

# Update [databases] section to point at the new primary's IP
sudo vim /etc/pgbouncer/pgbouncer.ini
# Reload (without dropping clients)
sudo -u postgres psql -h 127.0.0.1 -p 6432 -U pgbouncer pgbouncer -c "RELOAD;"
# Then force pools to reconnect
sudo -u postgres psql -h 127.0.0.1 -p 6432 -U pgbouncer pgbouncer -c "RECONNECT;"

If your application connects directly with a single hostname in DNS:

# Update the A record to point at the new primary
# This requires waiting for TTL — short TTLs (30-60s) on the DB hostname are the recommended practice

Step 5 — Restart application workers

Most application connection pools cache successful connection sockets. After fencing the primary, those sockets are dead but the pool doesn’t know yet. Force a recycle:

# Rolling-restart the application pods/processes
# Example for systemd-managed app
sudo systemctl restart app@*

# Example for Kubernetes
kubectl rollout restart deployment/app -n production

Watch the application log for the first successful write. That’s your end-of-incident timestamp.

Step 6 — Rebuild the old primary as a new standby

This is what you do after the immediate outage, not during. The old primary, once recovered, cannot just be “started back up” — it’s on the wrong timeline now. Use pg_rewind if its WAL is intact, or full pg_basebackup if it isn’t:

# On the old primary (now-defunct), assuming WAL is intact and you have the new primary reachable
sudo -u postgres /usr/pgsql-16/bin/pg_rewind \
  --target-pgdata=/var/lib/pgsql/16/data \
  --source-server="host=<new-primary> port=5432 user=postgres dbname=postgres" \
  --progress

# Then configure as a standby (create standby.signal, set primary_conninfo) and start

See the timeline-divergence runbook for the decision tree between pg_rewind and full re-baseline.

Step 7 — Postmortem

Manual promotion is not a thing that should happen often. Every event should produce three artifacts:

  • The exact LSN gap between received and replayed at the moment of fence — your actual RPO for this incident.
  • The wall-clock interval between primary death and first successful write through the application — your actual RTO.
  • A decision on whether to automate (Patroni, repmgr, pg_auto_failover) before the next event.

If you do this twice in a year, automate. Manual promotion is fine for clusters where the team has a strong reason to keep humans in the loop — compliance, exotic failure modes, or sites with operators always on shift. For most B2B SaaS workloads, the human in the loop is what’s actually slow.

Run Postgres under our Cloud Compute tier with automated failover (Patroni) baked in, or book a scoping call if you want us to operate the manual-promotion runbook as part of monitoring and on-call.