Question: How can you set up a PostgreSQL cluster using Docker?


Setting up a PostgreSQL cluster using Docker involves multiple steps including defining the Docker network, setting up individual PostgreSQL containers, and configuring them to work together. Here's a step-by-step guide:

Step 1: Create a Docker Network

First, create a Docker network that will allow the containers to communicate with each other.

docker network create pgnet

Step 2: Start the PostgreSQL Master Container

Start the master PostgreSQL container. You'll need to specify environment variables to set up the initial database settings.

docker run -d \ --name pgmaster \ --network pgnet \ -e POSTGRES_PASSWORD=mysecretpassword \ -e POSTGRES_USER=myuser \ -e POSTGRES_DB=mydb \ postgres

Step 3: Start the PostgreSQL Slave Containers

Next, start one or more slave containers. PostgreSQL replication requires configuring the slaves to follow the master. This example uses environment variables and assumes you are using a custom Docker image that has replication configured, or you can mount a script that sets up replication in the official PostgreSQL image.

docker run -d \ --name pgslave1 \ --network pgnet \ -e POSTGRES_PASSWORD=mysecretpassword \ -e POSTGRES_USER=myuser \ -e POSTGRES_DB=mydb \ postgres

You would replicate this command for as many slaves as you need, changing the name (pgslave1, pgslave2, etc.).

Step 4: Configure Replication

To configure replication, you'll need to customize the PostgreSQL configuration files on both master and slaves. For the master, settings like wal_level, max_wal_senders, and hot_standby need to be adjusted. For the slaves, you must set up connection information to the master, typically in a recovery.conf file or equivalent depending on your PostgreSQL version.

Step 5: Verify Cluster Operation

After all instances are running and configured, verify that the replication is working correctly by checking the slave databases to ensure they are receiving updates from the master.

Use commands like psql to connect to the master and slave instances and inspect replication status and data consistency.

This setup provides a basic example of how to establish a PostgreSQL cluster using Docker. In production scenarios, consider using Docker Compose or Kubernetes for better manageability, and make sure to handle data persistence, backup, failover procedures, and security aspects appropriately.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book
Start building today

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.