Production-ready PostgreSQL image with:
This README is a Docker Hub consumer guide.
docker pull nextstage/postgres-ha:18
docker run -d --name postgres-ha \
-e POSTGRES_PASSWORD='StrongPasswordHere' \
-p 5432:5432 \
-v pgdata:/var/lib/postgresql/18/docker \
nextstage/postgres-ha:18
Test connection:
psql "postgresql://postgres:[email protected]:5432/postgres"
| Variable | Default | Notes |
|---|---|---|
POSTGRES_USER | postgres | Main admin user. |
POSTGRES_PASSWORD | NOTDEFINED | Set a strong password in production. |
PG_PASSWORD_ENCRYPTION | scram-sha-256 | Auth method used in generated pg_hba.conf. |
REPLICATION_MODE | master | master or slave. |
REPLICATION_USER | replicator | Replication user name. |
REPLICATION_PASSWORD | NONE:16 | Must be explicitly set for replication to work. |
REPLICATION_HOST | (empty) | Required on standby (slave) to point to primary host/service. |
REPLICATION_PORT | 5432 | Primary PostgreSQL port used by standby. |
| Path in container | Purpose |
|---|---|
/var/lib/postgresql/18/docker | Main data directory (PG18+). Persist this path. |
/etc/postgresql/18/main/conf.d | Extra PostgreSQL config files. |
/to-restore | Optional backups and seed files (*.backup, *.seed.sql). |
/pg_tablespace/index | Optional index tablespace disk. |
/pg_tablespace/tmp | Optional temp tablespace disk. |
services:
db_master:
image: nextstage/postgres-ha:18
container_name: db_master_18
restart: always
environment:
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
REPLICATION_MODE: master
PG_PASSWORD_ENCRYPTION: scram-sha-256
REPLICATION_USER: replicator
REPLICATION_PASSWORD: ${REPLICATION_PASSWORD}
ports:
- "5432:5432"
volumes:
- ./persist/master:/var/lib/postgresql/18/docker
- ./conf/master:/etc/postgresql/18/main/conf.d
- ./backups:/to-restore
healthcheck:
test: ["CMD-SHELL", "/scripts/healthcheck.sh"]
interval: 10s
timeout: 5s
retries: 30
services:
db_master:
image: nextstage/postgres-ha:18
container_name: db_master_18
environment:
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
REPLICATION_MODE: master
REPLICATION_USER: replicator
REPLICATION_PASSWORD: ${REPLICATION_PASSWORD}
ports:
- "5432:5432"
volumes:
- ./persist/master:/var/lib/postgresql/18/docker
healthcheck:
test: ["CMD-SHELL", "/scripts/healthcheck.sh"]
interval: 10s
timeout: 5s
retries: 60
db_slave:
image: nextstage/postgres-ha:18
container_name: db_slave_18
depends_on:
db_master:
condition: service_healthy
environment:
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
REPLICATION_MODE: slave
REPLICATION_USER: replicator
REPLICATION_PASSWORD: ${REPLICATION_PASSWORD}
REPLICATION_HOST: db_master
REPLICATION_PORT: 5432
ports:
- "5433:5432"
volumes:
- ./persist/slave:/var/lib/postgresql/18/docker
docker exec -it db_slave_18 bash /scripts/promoteToMaster.sh
After promotion, update deployment variables so this node runs with REPLICATION_MODE=master.
When a database is created (for example with /scripts/createDatabase.sh), the image checks
/to-restore for files that match the database name:
/to-restore/<database>.backup/to-restore/<database>.seed.sqlBehavior:
<database>.backup when present<database>.seed.sql when presentPOSTGRES_PASSWORD and REPLICATION_PASSWORDContent type
Image
Digest
sha256:648bcf93a…
Size
255.3 MB
Last updated
8 days ago
docker pull nextstage/postgres:18-ha