PostgreSQL streaming replication is the foundation of every Postgres HA design — from manual promotion to Patroni-managed clusters. The primary writes WAL, the standby’s walreceiver pulls it over a replication connection, and the standby’s startup process replays it. This article walks the setup on Postgres 16, the pg_basebackup bootstrap, replication-slot configuration that prevents WAL retention surprises, and the queries that prove the topology is healthy.
How to verify
On a working primary-standby pair, three queries tell you the state. Run on the standby:
-- Confirm we are in recovery
SELECT pg_is_in_recovery();
-- t
-- Last received and replayed WAL positions
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 replay_lag_bytes;
-- Connection to primary
SELECT status, sender_host, sender_port,
extract(epoch from now() - last_msg_receipt_time) AS seconds_since_msg
FROM pg_stat_wal_receiver;
On the primary, see who is streaming from it:
SELECT client_addr, state, sent_lsn, write_lsn, flush_lsn, replay_lsn,
extract(epoch from write_lag) AS write_lag_s,
extract(epoch from replay_lag) AS replay_lag_s
FROM pg_stat_replication;
What’s happening
PostgreSQL’s WAL (Write-Ahead Log) records every change before it hits the data files. Streaming replication has the primary stream those WAL records to the standby over a libpq connection; the standby’s walreceiver writes them locally, and the startup process applies them. The standby is in continuous recovery mode — readable for hot_standby SELECT queries but not writable.
Asynchronous is the default — the primary commits without waiting for the standby. The lag between primary commit and standby apply (measured in pg_stat_replication.replay_lag) is your RPO at any given moment. For synchronous, set synchronous_commit = on and synchronous_standby_names; the primary then waits for at least one standby to confirm the WAL is durable before returning to the client.
Replication slots are persistent server-side state on the primary that records which WAL segments a particular standby has consumed. With a slot, the primary will retain WAL until the standby acknowledges it — protecting against the standby being offline and missing WAL. Without a slot, WAL beyond wal_keep_size is recycled and a long-offline standby cannot catch up without re-baseline.
The procedure
-
Configure the primary. In
/etc/postgresql/16/main/postgresql.conf:listen_addresses = '*' wal_level = replica max_wal_senders = 10 max_replication_slots = 10 wal_keep_size = 4GB synchronous_commit = on # change to 'remote_apply' for the strongest semanticsIn
pg_hba.conf:hostssl replication repl 10.0.0.6/32 scram-sha-256Restart:
sudo systemctl restart postgresql@16-main. -
Create the replication user on the primary.
CREATE ROLE repl WITH REPLICATION LOGIN PASSWORD 'strong-password'; -
Create a physical replication slot for this standby.
SELECT pg_create_physical_replication_slot('standby_1'); -
Bootstrap the standby with pg_basebackup. On the standby host, stop any running cluster first.
sudo systemctl stop postgresql@16-main sudo -u postgres rm -rf /var/lib/postgresql/16/main/* PGPASSWORD='strong-password' sudo -u postgres pg_basebackup \ -h 10.0.0.5 -p 5432 -U repl \ -D /var/lib/postgresql/16/main \ -Fp -X stream -P -R \ -S standby_1What each flag does:
-Fp— plain format (preserves the directory structure).-X stream— stream WAL in parallel during backup so the standby is ready to start immediately.-R— writestandby.signaland aprimary_conninfoline so the standby boots as a standby.-S standby_1— use the replication slot we created.
-
Adjust
postgresql.auto.confon the standby for the right host and TLS:primary_conninfo = 'host=10.0.0.5 port=5432 user=repl password=strong-password sslmode=verify-full sslrootcert=/etc/ssl/certs/ca.crt application_name=standby_1' primary_slot_name = 'standby_1' -
Confirm
standby.signalexists. Its presence tells the postmaster to enter recovery.ls -la /var/lib/postgresql/16/main/standby.signal -
Start the standby.
sudo systemctl start postgresql@16-main sudo tail -50 /var/log/postgresql/postgresql-16-main.logYou should see lines like
started streaming WAL from primary at 0/3000000 on timeline 1. -
Verify replication on both ends. On the primary:
SELECT client_addr, application_name, state, replay_lag FROM pg_stat_replication;On the standby:
SELECT pg_is_in_recovery(); SELECT pg_last_wal_replay_lsn(); -
Make the standby readable (for query offload) by setting
hot_standby = onin the standby’spostgresql.conf— it is on by default in PG 10+. Confirm:SHOW hot_standby;
Common pitfalls
- A standby without a replication slot will fall behind silently when WAL recycles past
wal_keep_size, then refuse to reconnect with “requested WAL segment has already been removed.” Always use slots for standbys you intend to keep. - A slot with no consumer accumulates WAL forever and fills the primary’s disk. Monitor
pg_replication_slots.active = falseslots and drop or fix them. pg_basebackup -X streamneeds a second walsender slot beyond the slot you defined — setmax_wal_sendersto a number that covers all standbys plus all in-flight backups.- Synchronous replication with a single standby that goes offline halts writes on the primary — you need at least two synchronous candidates and a quorum config, or you accept that primary writes pause when the standby is down.
- A standby that drifts to a different timeline (because someone promoted and then un-promoted) cannot reconnect with
pg_rewind’s help only — sometimes you need a full re-baseline withpg_basebackup.
In the engagements we run, PostgreSQL replication topologies are managed by Patroni for the HA tier and by Ansible-driven manual configuration for the simpler read-replica patterns. We monitor lag through postgres_exporter and treat any sustained replication lag over 30 seconds as a paging condition. We carry that under Managed Operations — and the manual-promotion runbook lives in /en/knowledge-base/cluster-failover-postgres-manual-promotion/ for the cases where automation is intentionally absent.