Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 187Data · PostgreSQL

How to set up PostgreSQL streaming replication

One database server is one disk failure away from downtime. Streaming replication keeps a second server (the standby) as an exact, read-only copy: the primary sends its write-ahead log (WAL) live, and the standby replays it within seconds. The standby can serve read queries and be promoted to primary if the main server dies. This how-to sets up a standby with pg_basebackup, checks the lag, and does a planned failover.

  • 5 Lessen
  • 4 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

187.1

The quick answer

On the primary (10.0.0.11):

sudo -u postgres psql -c "CREATE ROLE replicator WITH REPLICATION LOGIN;"
sudo -u postgres psql -c "\password replicator"
sudo -u postgres psql -c "SELECT pg_create_physical_replication_slot('standby1');"

On the standby (10.0.0.12), with PostgreSQL installed and stopped:

systemctl stop postgresql
rm -rf /var/lib/postgresql/16/main/*
sudo -u postgres pg_basebackup -h 10.0.0.11 -U replicator -D /var/lib/postgresql/16/main -R -X stream -S standby1 -P
systemctl start postgresql

187.2

Step by step

  1. 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.
  2. Primary settings — the defaults already fit (wal_level = replica, max_wal_senders = 10). Make it listen on the private address in postgresql.conf: listen_addresses = 'localhost,10.0.0.11'.
  3. Allow the standby in pg_hba.conf (only its IP, TLS and SCRAM), then reload:
/etc/postgresql/16/main/pg_hba.conf (add)
hostssl  replication  replicator  10.0.0.12/32  scram-sha-256
sudo -u postgres psql -c "SELECT pg_reload_conf();"
  1. 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';.
  2. Clone the primary (quick answer, standby part) — -R writes standby.signal and primary_conninfo, -X stream copies WAL during the backup, -S uses the slot. Store the password in ~postgres/.pgpass (mode 600) instead of typing it.
  3. 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;"
  1. Planned failover — stop writes (app in maintenance), wait for lag 0, stop the primary, then promote the standby:
sudo -u postgres psql -c "SELECT pg_promote();"

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.

  1. Automatic failover — use Patroni (with etcd) or a managed service; do not script promotion yourself. Replication is not a backup: a DROP TABLE is replicated too.

187.3

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.

187.4

Common problems

ProblemCauseFix
no pg_hba.conf entry for replication connectionMissing or wrong replication line.Add the line for the standby IP; reload.
pg_basebackup: directory exists but is not emptyOld data in the target.Stop PostgreSQL; empty the data directory.
Primary disk fills with WALSlot of a dead standby keeps WAL.max_slot_wal_keep_size; drop unused slots (pg_drop_replication_slot).
Standby lag growsNetwork, 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 primaryOld primary started again after failover.Fence it (stop service, block port); rebuild as standby.

187.5

Go further

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.

Alle commando's in dit hoofdstuk