diff --git a/docker-entrypoint-initdb.d/00-main.sh b/docker-entrypoint-initdb.d/00-main.sh index 5791a46..5a0c17c 100755 --- a/docker-entrypoint-initdb.d/00-main.sh +++ b/docker-entrypoint-initdb.d/00-main.sh @@ -1,8 +1,17 @@ #!/usr/bin/env bash +source /usr/local/bin/docker-entrypoint.sh + # the variable UPSTREAM indicates that a standby instance is requested, otherwise this is primary if [[ -z $UPSTREAM ]]; then + psql -c "CREATE USER repmgr WITH SUPERUSER ENCRYPTED PASSWORD '${REPLICATOR_PASSWORD}';" + psql -c "CREATE DATABASE repmgr WITH OWNER repmgr;" + psql -c 'ALTER USER repmgr SET search_path TO repmgr, "$user", public;' + + docker_temp_server_stop + + # ensure required entries in configuration # shellcheck disable=SC2129 # echo "archive_command = 'cp %p ${WAL_ARCHIVE:-/wal_archive}/%f'" >> /var/lib/postgresql/data/postgresql.conf @@ -14,54 +23,56 @@ if [[ -z $UPSTREAM ]]; then echo "max_wal_senders = 10" >> /var/lib/postgresql/data/postgresql.conf echo "max_replication_slots = 10" >> /var/lib/postgresql/data/postgresql.conf # ensure required entries in hba - echo "local all all trust" > /var/lib/postgresql/data/pg_hba.conf - echo "local replication repmgr trust" >> /var/lib/postgresql/data/pg_hba.conf - echo "host replication repmgr 127.0.0.1/32 trust" >> /var/lib/postgresql/data/pg_hba.conf - echo "host replication repmgr 100.64.0.0/10 trust" >> /var/lib/postgresql/data/pg_hba.conf - echo "local repmgr repmgr trust" >> /var/lib/postgresql/data/pg_hba.conf - echo "host repmgr repmgr 127.0.0.1/32 trust" >> /var/lib/postgresql/data/pg_hba.conf - echo "host repmgr repmgr 100.64.0.0/10 trust" >> /var/lib/postgresql/data/pg_hba.conf + echo "local all all trust" > /var/lib/postgresql/data/pg_hba.conf + echo "local replication repmgr trust" >> /var/lib/postgresql/data/pg_hba.conf + echo "host replication repmgr 127.0.0.1/32 trust" >> /var/lib/postgresql/data/pg_hba.conf + echo "host replication repmgr 100.64.0.0/10 trust" >> /var/lib/postgresql/data/pg_hba.conf + echo "local repmgr repmgr trust" >> /var/lib/postgresql/data/pg_hba.conf + echo "host repmgr repmgr 127.0.0.1/32 trust" >> /var/lib/postgresql/data/pg_hba.conf + echo "host repmgr repmgr 100.64.0.0/10 trust" >> /var/lib/postgresql/data/pg_hba.conf echo "host all all all scram-sha-256" >> /var/lib/postgresql/data/pg_hba.conf - -# echo "local replication repmgr trust" >> /var/lib/postgresql/data/pg_hba.conf -# echo "host replication repmgr ${NETWORK:-100.64.0.0/10} trust" >> /var/lib/postgresql/data/pg_hba.conf -# echo "local repmgr repmgr trust" >> /var/lib/postgresql/data/pg_hba.conf -# echo "host repmgr repmgr ${NETWORK:-100.64.0.0/10} trust" >> /var/lib/postgresql/data/pg_hba.conf # create user for replication - psql -c "CREATE USER repmgr WITH SUPERUSER ENCRYPTED PASSWORD '${REPLICATOR_PASSWORD}';" - psql -c "CREATE DATABASE repmgr WITH OWNER repmgr;" - psql -c 'ALTER USER repmgr SET search_path TO repmgr, "$user", public;' + echo "node_id = 1" >> /etc/repmgr.conf + echo "node_name = pg-0" >> /etc/repmgr.conf + echo "conninfo = 'host=pg-0 dbname=repmgr user=repmgr connect_timeout=2'" >> /etc/repmgr.conf + echo "data_directory = '/var/lib/postgresql/data'" >> /etc/repmgr.conf + echo "use_replication_slots = on" >> /etc/repmgr.conf + echo "pg_bindir = /usr/lib/postgresql/15/bin/" >> /etc/repmgr.conf - echo "node_id = 1" >> /etc/repmgr.conf - echo "node_name = pg-0" >> /etc/repmgr.conf - echo "conninfo ='host=pg-0 dbname=repmgr user=repmgr connect_timeout=2'" >> /etc/repmgr.conf - echo "data_directory = '/var/lib/postgresql/data'" >> /etc/repmgr.conf # process selected tweaks if [[ -n $MAX_CONNECTIONS ]]; then echo "max_connections = ${MAX_CONNECTIONS}" >> /var/lib/postgresql/data/postgresql.conf fi + docker_temp_server_start + + /usr/lib/postgresql/15/bin/repmgr -f /etc/repmgr.conf primary register + + else # this is a standby - source /usr/local/bin/docker-entrypoint.sh docker_temp_server_stop rm -rf /var/lib/postgresql/data/* + echo "node_id = 2" >> /etc/repmgr.conf + echo "node_name = pg-1" >> /etc/repmgr.conf + echo "conninfo = 'host=pg-1 dbname=repmgr user=repmgr connect_timeout=2'" >> /etc/repmgr.conf + echo "data_directory = '/var/lib/postgresql/data'" >> /etc/repmgr.conf + echo "use_replication_slots = on" >> /etc/repmgr.conf + echo "pg_bindir = /usr/lib/postgresql/15/bin/" >> /etc/repmgr.conf - echo "node_id = 2" >> /etc/repmgr.conf - echo "node_name = pg-1" >> /etc/repmgr.conf - echo "conninfo ='host=pg-1 dbname=repmgr user=repmgr connect_timeout=2'" >> /etc/repmgr.conf - echo "data_directory = '/var/lib/postgresql/data'" >> /etc/repmgr.conf + until /usr/lib/postgresql/15/bin/repmgr -h pg-0 -U repmgr -f /etc/repmgr.conf standby clone --dry-run + do + echo "Upstream host not ready..." + sleep 300 + done /usr/lib/postgresql/15/bin/repmgr -h pg-0 -U repmgr -f /etc/repmgr.conf standby clone -# OLD_PGPASSWORD=$PGPASSWORD -# export PGPASSWORD=$REPLICATOR_PASSWORD -# pg_basebackup -h "${UPSTREAM}" -U repmgr -D "${PGDATA}" -Fp -Xs -P -R -# export PGPASSWORD=$OLD_PGPASSWORD + docker_temp_server_start fi