PostgreSQL image with Primary & Replica support, plus SSL support. In order to build the image, run:
DOCKER_REPO=ghcr.io/mutablelogic/docker-postgres make dockerReplacing the DOCKER_REPO with the repository you want to push the image to.
The image can be run as a standalone instance, primary, or replica, depending on the environment variables passed on the docker command line:
POSTGRES_REPLICATION_PRIMARY: Required for replica The host and port that the replica will use to connect to the primary, in the formhost=<hostname> port=5432. When not set, the instance role is a primary or standalone.POSTGRES_REPLICATION_PASSWORD: Required for replication: The password for thePOSTGRES_REPLICATION_USER. If not set, replication is disabled and the instance runs as a standalone server.POSTGRES_REPLICATION_USER: Default isreplicator: The user that the replica will use to connect to the primary.POSTGRES_REPLICATION_SLOT: Default isreplica1: The replication slot for each replica. On the primary, this is a comma-separated list of replication slots. On a replica, this is the name of the replication slot used for synchronization.POSTGRES_DATABASES: Optional: A comma-separated list of databases (and associated owner role, which has the same name as the database), in addition to the main database.POSTGRES_PASSWORD_<role>: Optional: For any database which is created, you can enable login and set the password for the database owner role by setting this environment variable. Without this environment variable, the role will not be able to login.POSTGRES_SSL_CERT: Optional: The SSL certificate file location for the server, within the container. RequiresPOSTGRES_SSL_KEYto also be set.POSTGRES_SSL_KEY: Optional: The SSL private key file location for the server, within the container. RequiresPOSTGRES_SSL_CERTto also be set.POSTGRES_SSL_CA: Optional: The SSL CA certificate file location for client certificate verification. Only used whenPOSTGRES_SSL_CERTandPOSTGRES_SSL_KEYare set.
Important: PostgreSQL 18+ uses a different data directory structure to support pg_upgrade.
| Version | Volume mount path |
|---|---|
| 17 and earlier | -v myvolume:/var/lib/postgresql/data |
| 18 and later | -v myvolume:/var/lib/postgresql |
See docker-library/postgres#1259 for details.
Example of running a standalone PostgreSQL instance without replication:
PostgreSQL 17:
docker volume create postgres-data
docker run \
--rm --name postgres \
-e POSTGRES_PASSWORD="postgres" \
-p 5432:5432 \
-v postgres-data:/var/lib/postgresql/data \
ghcr.io/mutablelogic/docker-postgres:17-bookwormPostgreSQL 18+:
docker volume create postgres-data
docker run \
--rm --name postgres \
-e POSTGRES_PASSWORD="postgres" \
-p 5432:5432 \
-v postgres-data:/var/lib/postgresql \
ghcr.io/mutablelogic/docker-postgres:18-trixieThis gives you PostgreSQL with pg_stat_statements pre-loaded, plus PostGIS and pgvector available.
Example of running a primary instance, with two replication slots.
You should change the password for the POSTGRES_PASSWORD and POSTGRES_REPLICATION_PASSWORD
environment variables in this example:
PostgreSQL 17:
docker volume create postgres-primary
docker run \
--rm --name postgres-primary \
-e POSTGRES_REPLICATION_SLOT="replica1,replica2" \
-e POSTGRES_REPLICATION_PASSWORD="postgres" \
-e POSTGRES_PASSWORD="postgres" \
-p 5432:5432 \
-v postgres-primary:/var/lib/postgresql/data \
ghcr.io/mutablelogic/docker-postgres:17-bookwormPostgreSQL 18+:
docker volume create postgres-primary
docker run \
--rm --name postgres-primary \
-e POSTGRES_REPLICATION_SLOT="replica1,replica2" \
-e POSTGRES_REPLICATION_PASSWORD="postgres" \
-e POSTGRES_PASSWORD="postgres" \
-p 5432:5432 \
-v postgres-primary:/var/lib/postgresql \
ghcr.io/mutablelogic/docker-postgres:18-trixieYou can add additional replication slots later as needed.
When you run a replica instance, the first time it runs it will backup from the primary instance and then start
replication. You should change the password for the POSTGRES_PASSWORD and POSTGRES_REPLICATION_PASSWORD
environment variables, and set the POSTGRES_REPLICATION_PRIMARY environment variable to the primary instance
in this example:
PostgreSQL 17:
docker volume create postgres-replica1
docker run \
--rm --name postgres-replica1 \
-e POSTGRES_REPLICATION_PRIMARY="host=milou.lan port=5432" \
-e POSTGRES_REPLICATION_SLOT="replica1" \
-e POSTGRES_REPLICATION_PASSWORD="postgres" \
-e POSTGRES_PASSWORD="postgres" \
-p 5433:5432 \
-v postgres-replica1:/var/lib/postgresql/data \
ghcr.io/mutablelogic/docker-postgres:17-bookwormPostgreSQL 18+:
docker volume create postgres-replica1
docker run \
--rm --name postgres-replica1 \
-e POSTGRES_REPLICATION_PRIMARY="host=milou.lan port=5432" \
-e POSTGRES_REPLICATION_SLOT="replica1" \
-e POSTGRES_REPLICATION_PASSWORD="postgres" \
-e POSTGRES_PASSWORD="postgres" \
-p 5433:5432 \
-v postgres-replica1:/var/lib/postgresql \
ghcr.io/mutablelogic/docker-postgres:18-trixieA second replica (and so forth) can be run in the same way, but with a different port and volume name. You can run up to ten replicas by default. You should ensure the primary instance is running before starting the replica.
The docker images include the following extensions:
- pg_stat_statements - Pre-loaded for query
performance monitoring. Create the extension with
CREATE EXTENSION pg_stat_statements;to start collecting statistics. - PostGIS - Spatial database extender.
- pgvector - Vector similarity search.
You can raise issues and feature requests using the GitHub issue tracker or send pull requests. The image is built from the Official Docker image.