Question: How can you set up PostgreSQL replication using Docker?
Answer
Setting up PostgreSQL replication with Docker involves creating a primary (master) database server and one or more standby (replica) servers. The primary server is the source of data changes, and the standby servers replicate these changes, ensuring data redundancy and high availability. Here's a step-by-step guide to setting this up:
Step 1: Define Docker Network
First, create a custom bridge network in Docker. This facilitates communication between the containers.
docker network create --driver bridge pg_replication_net
Step 2: Start the Primary PostgreSQL Container
Run a PostgreSQL container as the primary server. Replace your_password
with a secure password.
docker run -d --name pg_primary \ --network pg_replication_net \ -e POSTGRES_PASSWORD=your_password \ postgres:latest
Step 3: Configure Replication on the Primary Server
- Access the primary server's shell.
docker exec -it pg_primary bash
- Switch to the postgres user.
su postgres
- Open the
postgresql.conf
file and set thewal_level
toreplica
,max_wal_senders
to a number greater than the expected replicas, andhot_standby
toon
.vi /var/lib/postgresql/data/postgresql.conf
- Add replication privileges to the
pg_hba.conf
file. This example allows any host in the network to replicate.echo "host replication all 172.18.0.0/16 md5" >> /var/lib/postgresql/data/pg_hba.conf
- Restart the primary server for changes to take effect.
pg_ctlcluster <version> main restart
Step 4: Create a Replication User
Create a user dedicated to replication processes.
CREATE ROLE replicator WITH REPLICATION LOGIN PASSWORD 'replica_password';
Step 5: Start the Standby PostgreSQL Container
Clone the primary to create a base backup for the standby server.
- On the primary server, start the backup.
pg_basebackup -h pg_primary -D /tmp/standby -U replicator -vP -W
- Copy the backup to the standby server (Here we simulate by creating another container).
- Start the standby PostgreSQL container with the copied data.
docker run -d --name pg_standby \ --network pg_replication_net \ -v /path/to/copied/data:/var/lib/postgresql/data \ postgres:latest
Now, configure the standby to follow the primary by creating a recovery configuration (standby.signal
and primary_conninfo
in PG12+).
Conclusion
You've now set up basic PostgreSQL replication using Docker. This configuration suits development and testing environments. For production, consider additional configurations like proper network security, more sophisticated setup with tools like Patroni for failover management, and continuous monitoring.
Was this content helpful?
Other Common PostgreSQL Questions (and Answers)
- How do you manage Postgres replication lag?
- How can I limit the number of rows updated in a PostgreSQL query?
- How does sharding work in PostgreSQL?
- How do you limit the number of rows deleted in PostgreSQL?
- How do you use the PARTITION OVER clause in PostgreSQL?
- What are PostgreSQL replication slots and how do they work?
- How can you partition an existing table in PostgreSQL?
- How do you partition a table by multiple columns in PostgreSQL?
- How do you check the replication status in PostgreSQL?
- What are the scaling limits of PostgreSQL?
- How do you scale Azure PostgreSQL?
- How do you use the limit clause in PostgreSQL to get the top N rows of a query result?
Free System Design on AWS E-Book
Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.
Switch & save up to 80%
Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost