Note: This is a basic demonstration of PostgreSQL replication concepts, not production-grade configuration.
Setup Overview
- Master: PostgreSQL primary accepting writes
- Standby: PostgreSQL replica replicating from master
- Method: Streaming replication with synchronous mode
- Tool: Docker containers for isolation
Step 1: Run Master Container
docker run --name pmaster -p 5432:5432 \
-v /Users/kaushik/Projects/Database/rep/pmaster_data:/var/lib/postgresql/18/docker \
-e POSTGRES_PASSWORD=postgres -d postgresStep 2: Run Standby Container
docker run --name pstandby -p 5433:5432 \
-v /Users/kaushik/Projects/Database/rep/pstandby_data:/var/lib/postgresql/18/docker \
-e POSTGRES_PASSWORD=postgres -d postgresStep 3: Prepare Standby for Replication
Stop Both Servers
docker stop pmaster pstandbyBackup and Replace Standby Data
# Backup existing standby data
mv pstandby_data pstandby_data_bk
# Copy master data to standby (base backup)
cp -R pmaster_data pstandby_dataStep 4: Configure Master for Replication
Edit pmaster_data/pg_hba.conf
Add at end of file:
host replication postgres all md5
Warning: Using postgres superuser for replication is not recommended in production. Create dedicated replication user.
Edit pmaster_data/postgresql.conf
Uncomment and set:
synchronous_standby_names = 'first 1 (standby_1)'
Step 5: Configure Standby for Replication
Edit pstandby_data/postgresql.conf
Uncomment and set:
primary_conninfo = 'application_name=standby_1 host=host.docker.internal port=5432 user=postgres password=postgres'
Create Standby Signal File
touch pstandby_data/standby.signalNote:
standby.signalfile tells PostgreSQL to start in standby mode.
Step 6: Start Servers
docker start pmaster
docker start pstandbyVerification
Check Replication Status on Master
SELECT * FROM pg_stat_replication;Test Replication
-- On master (port 5432)
CREATE TABLE test (id INT, name TEXT);
INSERT INTO test VALUES (1, 'Replication Test');
-- On standby (port 5433)
SELECT * FROM test; -- Should see the dataKey Configuration Points
Master Configuration
- pg_hba.conf: Allow replication connections
- synchronous_standby_names: Define synchronous standby servers
Standby Configuration
- primary_conninfo: Connection string to master
- standby.signal: File indicating standby mode
Important Notes
- Port Mapping: Master (5432), Standby (5433)
- Host Reference: Use
host.docker.internalfor container-to-host communication - Synchronous Mode: Master waits for standby confirmation before commit
- Data Directory: Standby starts with copy of master’s data directory
- Production: Use proper replication user, SSL, and monitoring
Summary
- Setup: Copy master data to standby, configure replication settings
- Replication Type: Streaming replication (WAL-based)
- Mode: Synchronous — with the default
synchronous_commit = on, commits are flushed on the standby (no data loss), but not necessarily applied, so standby reads can still be stale. Useremote_applyfor read-your-writes on the standby - Key Files:
pg_hba.conf,postgresql.conf,standby.signal