This repository provides an example of configuring and running two PostgreSQL instances—prod-db1 (source) and prod-db2 (target)—within Docker, using logical replication to migrate data efficiently and with minimal downtime.
- Logical Replication: Stream data changes from the old database (prod-db1) to the new database (prod-db2) in near real-time.
- Zero-Downtime Migration: Keep prod1 online and serving traffic while data is continuously replicated to prod2.
- Custom Configuration: Tune PostgreSQL parameters (e.g.,
wal_level
,shared_buffers
,maintenance_work_mem
) for optimal replication and performance. - Automated Setup: Initialization scripts and Docker Compose ensure that the publication on prod1 and the subscription on prod2 are created and managed automatically.
- Publication on prod1: Creates a logical publication that makes all tables available for replication.
- Subscription on prod2: Connects to prod1’s publication, taking an initial snapshot of data and then continuously applying changes as they occur.
- Cutover: After the data on prod2 is current and stable, switch your application’s traffic to prod2. No downtime is required, as prod1 remains fully operational until you’re ready to retire it.
- Configure: Adjust
prod1_postgresql.conf
and any environment variables as needed. - Run: Launch services with
docker-compose up -d
. - Verify: Check that prod2 receives data from prod1 by monitoring
pg_stat_subscription
on prod2. - Switch Over: Point your application to prod2 whenever you’re ready. Prod1 can then be decommissioned.
- Minimal Disruption: No need to stop your current database.
- Flexibility: Replicate all or selected tables, and easily integrate schema changes.
- Scalability: Logical replication can handle high-load environments with proper tuning.
This setup demonstrates a robust, low-latency migration strategy, ensuring your data moves from old to new without pausing your business operations. Enjoy seamless migrations and reduced downtime!