diff --git a/bin/tdocker b/bin/tdocker index bbc63aee..527424c4 100755 --- a/bin/tdocker +++ b/bin/tdocker @@ -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" diff --git a/compose/pgsql-replication.yml b/compose/pgsql-replication.yml new file mode 100644 index 00000000..fede94b8 --- /dev/null +++ b/compose/pgsql-replication.yml @@ -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: \ No newline at end of file diff --git a/pgsql/init/creare-replicator.sql b/pgsql/init/creare-replicator.sql new file mode 100644 index 00000000..59f74419 --- /dev/null +++ b/pgsql/init/creare-replicator.sql @@ -0,0 +1,4 @@ +CREATE ROLE replicator +WITH REPLICATION +LOGIN +PASSWORD 'replica_password'; \ No newline at end of file diff --git a/pgsql/my-postgres-hba.conf b/pgsql/my-postgres-hba.conf new file mode 100644 index 00000000..4495b464 --- /dev/null +++ b/pgsql/my-postgres-hba.conf @@ -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 \ No newline at end of file diff --git a/pgsql/my-postgres-master.conf b/pgsql/my-postgres-master.conf new file mode 100644 index 00000000..a712938e --- /dev/null +++ b/pgsql/my-postgres-master.conf @@ -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' \ No newline at end of file diff --git a/pgsql/replica-entrypoint.sh b/pgsql/replica-entrypoint.sh new file mode 100755 index 00000000..8482a403 --- /dev/null +++ b/pgsql/replica-entrypoint.sh @@ -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 \ No newline at end of file