This project sets up a PostgreSQL replication environment using Docker, with one master node and two slave nodes. It also includes a sample database schema for a store management system.
- PostgreSQL 14.5 containers
- Master-Slave replication with two slaves
- Sample store database schema
- Easy setup with Docker Compose
- Automated backup and initialization scripts
๐ postgresql-replication/
โ
โโโ ๐ docker-compose.yml
โโโ ๐ docker-init.sh
โโโ ๐ createdb.sql
โโโ ๐ backup-master.sh
โโโ ๐ create-replica-user.sh
โโโ ๐ init-slave.sh
โโโ ๐ init.sh
โ
โโโ ๐ init-script/
โ โโโ ๐ csv/
โ โ โโโ (various CSV files)
โ โโโ ๐ config/
โ โ โโโ ๐ pg_hba.conf
โ โ โโโ ๐ postgres.conf
โ โโโ ๐ slave-config/
โ โโโ ๐ slave2-config/
โ โโโ ๐ init.sh
โ
โโโ ๐ data/
โโโ ๐ data-slave/
โโโ ๐ data-slave2/
-
Clone this repository: git clone https://github.com/yourusername/postgresql-replication.git cd postgresql-replication
-
Run the initialization script: ./docker-init.sh
This script will:
- Clear any existing data
- Start the master PostgreSQL node
- Prepare replica configuration
- Start the slave nodes
postgres_master
: Master node (Port 5432)postgres_slave
: First slave node (Port 5433)postgres_slave2
: Second slave node (Port 5434)
The createdb.sql
file sets up a store
schema with various tables including manufacturers, categories, products, stores, customers, price changes, deliveries, purchases, and purchase items. It also creates a store_sales_summary
view.
The master-slave replication is set up automatically using the provided configuration files and initialization scripts:
create-replica-user.sh
: Creates replication users and replication slotsbackup-master.sh
: Performs base backups for both slave nodesinit-slave.sh
: Initializes slave configurationsinit.sh
: Orchestrates the entire initialization process
Replication users are created with encrypted passwords. Ensure to change the default passwords in a production environment.
This setup is intended for development and testing purposes. For production environments, please ensure proper security measures are in place and customize the configuration as needed.