This project is a simple, containerized demonstration of PostgreSQL streaming replication. It spins up a primary and a read‑only replica using Docker Compose so you can verify that changes on the primary are replicated to the replica.
- Docker and Docker Compose installed
psqlclient available on your machine (for connecting to the databases)
Clone the repo and run the commands below in order:
chmod +x primary/01-setup-hba.sh
docker compose up -d
# Verify that both containers are up and running
docker ps
docker logs pg-primary
docker logs pg-replica
# Connect Primary
psql -h localhost -p 5433 -U repl_user -d app_db
# Connect Replica
psql -h localhost -p 5434 -U repl_user -d app_db
# Try running the below command in Replica Instance
CREATE TABLE should_fail(id int);
# Should get error
ERROR: cannot execute CREATE TABLE in a read-only transaction
# Testing the Replication
# Insert data in primary
CREATE TABLE test_data (
id SERIAL PRIMARY KEY,
value TEXT
);
INSERT INTO test_data (value) VALUES ('from primary');
# View Data in Replica Instance
SELECT * FROM test_data;
# Insert another data in primary
INSERT INTO test_data (value) VALUES ('2nd insert from primary');
# Recheck Data in Replica Instance
SELECT * FROM test_data;
# Must return 2 rows
docker compose down
docker compose down -v- The replica runs in read‑only mode, so writes should fail as shown above.
- If you change the compose or SQL setup, re-run
docker compose down -vto remove volumes and start fresh.