Postgres Streaming Replication: 30-Minute Tutorial
Replication is the foundation of Postgres HA. Setup is fewer steps than people expect.
Step 1: Configure primary
Edit primary’s postgresql.conf: wal_level = replica, max_wal_senders = 10.
Restart primary.
Step 2: Take base backup
pg_basebackup -h primary -D /var/lib/pgsql/data -P -U replicator- Creates a clone of primary’s data.
Step 3: Start replica
Replica’s postgresql.conf set as standby (recovery.conf or signal file in PG12+).
Start replica; it streams from primary.
Step 4: Verify replication
On primary: SELECT * FROM pg_stat_replication; shows the replica.
On replica: read-only queries; cannot write.
Antipatterns
- Replica with no monitoring. Lag undetected.
- Manual failover only. Practice or use Patroni.
- Replica writes. Will not work; data drift if forced.
What to do this week
Three moves. (1) Run the tutorial end-to-end on your own laptop / sandbox. (2) Apply the pattern to one production workload. (3) Document the variations you needed; share with the team.