PostgreSQL Replication#
Streaming replication gives you a full binary copy for high availability and read scaling. Logical replication gives you selective table-level syncing between databases that can run different PostgreSQL versions.
Streaming Replication Setup#
Configure the Primary#
# postgresql.conf
wal_level = replica
max_wal_senders = 5
wal_keep_size = 1GBCreate a replication role and allow connections:
CREATE ROLE replicator WITH REPLICATION LOGIN PASSWORD 'repl-secret';# pg_hba.conf
host replication replicator 10.0.0.0/8 scram-sha-256Initialize the Standby#
sudo systemctl stop postgresql-16
sudo rm -rf /var/lib/postgresql/16/main/*
pg_basebackup -h primary-host -U replicator -D /var/lib/postgresql/16/main \
--checkpoint=fast --wal-method=stream -R -P
sudo chown -R postgres:postgres /var/lib/postgresql/16/main
sudo systemctl start postgresql-16The -R flag creates standby.signal and writes connection info to postgresql.auto.conf. The standby now continuously receives and replays WAL from the primary, accepting read-only queries by default.