Streaming Replication Setup
Configure PostgreSQL streaming replication with a primary and standby server for high availability and read scaling.
Prerequisites
- -Two PostgreSQL instances (primary and standby)
- -Network connectivity between instances
- -PostgreSQL 14+
Steps
Create a replication role on the primary
Create a dedicated user with replication privileges on the primary server.
Configure primary for replication
Set the required WAL and replication parameters on the primary.
These changes require a PostgreSQL restart to take effect.
Add replication entry to pg_hba.conf
Allow the standby server to connect for replication.
Replace 10.0.0.0/24 with the actual IP range of your standby server. Restart PostgreSQL after this change.
Create a base backup on the standby
Clone the primary database to the standby server using pg_basebackup.
The -R flag creates standby.signal and sets primary_conninfo in postgresql.auto.conf automatically.
Start the standby and verify replication
Start PostgreSQL on the standby and confirm it is receiving WAL from the primary.
Check replication status on the primary
Monitor replication lag and standby connection status from the primary.
Full Script
FAQ
Discussion
Loading comments...