Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
The quick answer
Step by step
- Same major version on both servers (for example 16), similar hardware, and time sync. Paths here are Debian/Ubuntu; on RHEL the data directory is
/var/lib/pgsql/16/data. - Primary settings — the defaults already fit (
wal_level = replica,max_wal_senders = 10). Make it listen on the private address inpostgresql.conf:listen_addresses = 'localhost,10.0.0.11'. - Allow the standby in
pg_hba.conf(only its IP, TLS and SCRAM), then reload:
hostssl replication replicator 10.0.0.12/32 scram-sha-256- Replication user and slot (quick answer) — the slot makes the primary keep WAL until the standby has it. Limit it so a dead standby cannot fill the disk:
ALTER SYSTEM SET max_slot_wal_keep_size = '20GB';. - Clone the primary (quick answer, standby part) —
-Rwritesstandby.signalandprimary_conninfo,-X streamcopies WAL during the backup,-Suses the slot. Store the password in~postgres/.pgpass(mode 600) instead of typing it. - Check the replication — on the primary:
sudo -u postgres psql -x -c "SELECT client_addr, state, sync_state, write_lag, replay_lag FROM pg_stat_replication;"On the standby:
sudo -u postgres psql -c "SELECT pg_is_in_recovery(), now() - pg_last_xact_replay_timestamp() AS lag;"- Planned failover — stop writes (app in maintenance), wait for lag 0, stop the primary, then promote the standby:
Point the app (or a DNS name / virtual IP) to the new primary. The old primary must be rebuilt as a standby (pg_rewind or a new pg_basebackup) before it joins again.
- Automatic failover — use Patroni (with etcd) or a managed service; do not script promotion yourself. Replication is not a backup: a
DROP TABLEis replicated too.
Check that it worked
pg_stat_replication on the primary shows the standby with state = streaming; pg_is_in_recovery() on the standby returns t; a row inserted on the primary is visible on the standby within a second; a write on the standby fails with cannot execute INSERT in a read-only transaction.
Common problems
| Problem | Cause | Fix |
|---|---|---|
no pg_hba.conf entry for replication connection | Missing or wrong replication line. | Add the line for the standby IP; reload. |
pg_basebackup: directory exists but is not empty | Old data in the target. | Stop PostgreSQL; empty the data directory. |
| Primary disk fills with WAL | Slot of a dead standby keeps WAL. | max_slot_wal_keep_size; drop unused slots (pg_drop_replication_slot). |
| Standby lag grows | Network, slow disk, or long queries on the standby. | Check I/O; hot_standby_feedback; bigger standby. |
| Query on standby cancelled ("conflict with recovery") | Replay needed rows the query was reading. | max_standby_streaming_delay; run long reports elsewhere. |
| Both servers think they are primary | Old primary started again after failover. | Fence it (stop service, block port); rebuild as standby. |
Go further
- Command pages: psql, systemctl.
- Next how-tos: Restore PostgreSQL to a point in time, Back up and restore PostgreSQL, Create a PostgreSQL user and database, Open a port in the firewall.
- Learn it: Data services at work — PostgreSQL, Data services — backups. Full guide: PostgreSQL.
1What does the -R option of pg_basebackup do?
It writes standby.signal and the primary_conninfo setting, so the copy starts as a standby that follows the primary.
2Why is a replica not a backup?
Every change — also a wrong DELETE or DROP TABLE — is copied to the replica within seconds.