Skip to content

Commit 017ac9e

Browse files
committed
Made replication optional
1 parent 92a7c20 commit 017ac9e

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

README.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ Replacing the `DOCKER_REPO` with the repository you want to push the image to.
1111

1212
## Environment variables
1313

14-
The image can be run as a primary or replica, depending on the environment variables passed on the
14+
The image can be run as a standalone instance, primary, or replica, depending on the environment variables passed on the
1515
docker command line:
1616

1717
* `POSTGRES_REPLICATION_PRIMARY`: **Required for replica** The host and port that the replica will use
1818
to connect to the primary, in the form `host=<hostname> port=5432`. When not set,
19-
the instance role is a primary.
20-
* `POSTGRES_REPLICATION_PASSWORD`: **Required**: The password for the `POSTGRES_REPLICATION_USER`.
19+
the instance role is a primary or standalone.
20+
* `POSTGRES_REPLICATION_PASSWORD`: **Required for replication**: The password for the `POSTGRES_REPLICATION_USER`.
21+
If not set, replication is disabled and the instance runs as a standalone server.
2122
* `POSTGRES_REPLICATION_USER`: **Default is `replicator`**: The user that the replica will use to connect to the primary.
2223
* `POSTGRES_REPLICATION_SLOT`: **Default is `replica1`**: The replication slot for each replica.
2324
On the primary, this is a comma-separated list of replication slots. On a replica, this is the name
@@ -34,6 +35,22 @@ docker command line:
3435
* `POSTGRES_SSL_CA`: **Optional**: The SSL CA certificate file location for client certificate verification.
3536
Only used when `POSTGRES_SSL_CERT` and `POSTGRES_SSL_KEY` are set.
3637

38+
## Running a standalone server
39+
40+
Example of running a standalone PostgreSQL instance without replication:
41+
42+
```bash
43+
docker volume create postgres-data
44+
docker run \
45+
--rm --name postgres \
46+
-e POSTGRES_PASSWORD="postgres" \
47+
-p 5432:5432 \
48+
-v postgres-data:/var/lib/postgresql/data \
49+
ghcr.io/mutablelogic/docker-postgres:17-bookworm
50+
```
51+
52+
This gives you PostgreSQL with `pg_stat_statements` pre-loaded, plus PostGIS and pgvector available.
53+
3754
## Running a Primary server
3855

3956
Example of running a primary instance, with two replication slots.

scripts/10_primary.sh

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/bash
2-
# Script runs on a primary instance to set up replication slots
2+
# Script runs on a primary instance to set up replication slots and pg_stat_statements
33
# The primary instance should be started before the replicas
44
set -e
55

@@ -9,12 +9,19 @@ if [ ! -z "${POSTGRES_REPLICATION_PRIMARY}" ]; then
99
exit 0
1010
fi
1111

12-
# Validate required environment variables
12+
CONF="${PGDATA}/postgresql.conf"
13+
14+
# Enable pg_stat_statements extension (always)
15+
sed -i -e"s/^#shared_preload_libraries.*$/shared_preload_libraries = 'pg_stat_statements'/" ${CONF}
16+
echo "pg_stat_statements.max = 1000" >> ${CONF}
17+
18+
# Skip replication setup if no password is set
1319
if [ -z "${POSTGRES_REPLICATION_PASSWORD}" ]; then
14-
echo "POSTGRES_REPLICATION_PASSWORD needs to be set."
15-
exit 1
20+
echo "No POSTGRES_REPLICATION_PASSWORD set, skipping replication setup."
21+
exit 0
1622
fi
1723

24+
# Validate replication environment variables
1825
if [ -z "${POSTGRES_REPLICATION_USER}" ]; then
1926
echo "POSTGRES_REPLICATION_USER needs to be set."
2027
exit 1
@@ -49,14 +56,9 @@ EOSQL
4956
done
5057

5158
# Set configuration for replication
52-
CONF="${PGDATA}/postgresql.conf"
5359
sed -i -e"s/^#wal_level.*$/wal_level = replica/" ${CONF}
5460
sed -i -e"s/^#max_wal_senders.*$/max_wal_senders = 10/" ${CONF}
5561
sed -i -e"s/^#max_replication_slots.*$/max_replication_slots = 10/" ${CONF}
5662

57-
# Enable pg_stat_statements extension
58-
sed -i -e"s/^#shared_preload_libraries.*$/shared_preload_libraries = 'pg_stat_statements'/" ${CONF}
59-
echo "pg_stat_statements.max = 1000" >> ${CONF}
60-
6163
# Add a replication user to pg_hba.conf
6264
echo "host replication ${POSTGRES_REPLICATION_USER} all scram-sha-256" >> "${PGDATA}/pg_hba.conf"

0 commit comments

Comments
 (0)