Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/tdocker
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ files=(
"compose/nginx.yml"
"compose/otel-lgtm.yml"
"compose/pgsql.yml"
"compose/pgsql-replication.yml"
"compose/php.yml"
"compose/php-legacy.yml"
"compose/selenium.yml"
Expand Down
48 changes: 48 additions & 0 deletions compose/pgsql-replication.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
services:

pgsql-primary:
image: postgres:16-alpine
container_name: totara_pgsql-primary
ports:
- "54320:5432"
environment:
TZ: ${TIME_ZONE}
PGDATA: /var/lib/postgresql/data/pgdata
POSTGRES_HOST_AUTH_METHOD: trust
command:
- postgres
- -c
- config_file=/etc/postgresql/postgresql.conf
volumes:
- ./pgsql/my-postgres-master.conf:/etc/postgresql/postgresql.conf
- ./pgsql/my-postgres-hba.conf:/etc/postgresql/pg_hba.conf
- ./pgsql/init:/docker-entrypoint-initdb.d
- pgsql16-master-data:/var/lib/postgresql/data
depends_on:
- nginx
networks:
- totara

pgsql-replica-1:
image: postgres:16-alpine
container_name: totara_pgsql-replica-1
ports:
- "54321:5432"
environment:
TZ: ${TIME_ZONE}
PGDATA: /var/lib/postgresql/data/pgdata
POSTGRES_HOST_AUTH_METHOD: trust
entrypoint:
- /usr/local/bin/replica-entrypoint.sh
volumes:
- ./pgsql/my-postgres.conf:/etc/postgresql/postgresql.conf
- ./pgsql/replica-entrypoint.sh:/usr/local/bin/replica-entrypoint.sh
- pgsql16-replica-data-1:/var/lib/postgresql/data
depends_on:
- pgsql-primary
networks:
- totara

volumes:
pgsql16-master-data:
pgsql16-replica-data-1:
4 changes: 4 additions & 0 deletions pgsql/init/creare-replicator.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE ROLE replicator
WITH REPLICATION
LOGIN
PASSWORD 'replica_password';
13 changes: 13 additions & 0 deletions pgsql/my-postgres-hba.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
# TYPE DATABASE USER ADDRESS METHOD
host replication replicator 0.0.0.0/0 scram-sha-256
# Dev-only: allow connections from the host and other containers on the
# docker network. Mirrors POSTGRES_HOST_AUTH_METHOD=trust, which the
# mounted hba_file would otherwise override.
host all all 0.0.0.0/0 trust
host all all ::/0 trust
6 changes: 6 additions & 0 deletions pgsql/my-postgres-master.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
listen_addresses = '*'
wal_level = replica
max_wal_senders = 10
max_replication_slots = 10
hot_standby = on
hba_file = '/etc/postgresql/pg_hba.conf'
31 changes: 31 additions & 0 deletions pgsql/replica-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/sh
set -e

PRIMARY_HOST=pgsql-primary
REPLICATION_USER=replicator
PGDATA=${PGDATA:-/var/lib/postgresql/data}

if [ ! -f "$PGDATA/PG_VERSION" ]; then
echo "Initializing replica..."

until pg_isready -h "$PRIMARY_HOST" -U "$REPLICATION_USER"
do
sleep 2
done

rm -rf "$PGDATA"/*

export PGPASSWORD=replica_password

pg_basebackup \
-h "$PRIMARY_HOST" \
-U "$REPLICATION_USER" \
-D "$PGDATA" \
-R \
-Fp \
-Xs \
-P
fi

exec docker-entrypoint.sh postgres \
-c config_file=/etc/postgresql/postgresql.conf
Loading