Skip to content

porwalarchit/Mock-Postgres-Replication-Behaviour

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

Postgres Replication Demo

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.

Prerequisites

  • Docker and Docker Compose installed
  • psql client available on your machine (for connecting to the databases)

Quick Start

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

Notes

  • 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 -v to remove volumes and start fresh.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages