Files
postgresrepmgr/docker-entrypoint-initdb.d/00-main.sh

85 lines
4.3 KiB
Bash
Raw Normal View History

2026-02-19 11:09:35 +01:00
#!/usr/bin/env bash
2026-02-19 13:55:38 +01:00
source /usr/local/bin/docker-entrypoint.sh
2026-02-19 11:09:35 +01:00
# the variable UPSTREAM indicates that a standby instance is requested, otherwise this is primary
if [[ -z $UPSTREAM ]]; then
2026-02-19 13:55:38 +01:00
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
2026-02-19 11:09:35 +01:00
# ensure required entries in configuration
# shellcheck disable=SC2129
# echo "archive_command = 'cp %p ${WAL_ARCHIVE:-/wal_archive}/%f'" >> /var/lib/postgresql/data/postgresql.conf
echo "archive_command = '/bin/true'" >> /var/lib/postgresql/data/postgresql.conf
echo "archive_mode = on" >> /var/lib/postgresql/data/postgresql.conf
echo "hot_standby = on" >> /var/lib/postgresql/data/postgresql.conf
# echo "promote_trigger_file = '/var/lib/postgresql/data/${TRIGGER_FILE:-i_am_primary}'" >> /var/lib/postgresql/data/postgresql.conf
# echo "wal_keep_size = ${WAL_KEEP_SIZE:-1GB}" >> /var/lib/postgresql/data/postgresql.conf
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
2026-02-19 13:55:38 +01:00
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
2026-02-19 13:01:34 +01:00
echo "host all all all scram-sha-256" >> /var/lib/postgresql/data/pg_hba.conf
2026-02-19 12:53:07 +01:00
2026-02-19 11:09:35 +01:00
# create user for replication
2026-02-19 13:55:38 +01:00
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
2026-02-19 14:00:40 +01:00
echo "pg_bindir = '/usr/lib/postgresql/15/bin/'" >> /etc/repmgr.conf
2026-02-19 11:09:35 +01:00
# process selected tweaks
if [[ -n $MAX_CONNECTIONS ]]; then
echo "max_connections = ${MAX_CONNECTIONS}" >> /var/lib/postgresql/data/postgresql.conf
fi
2026-02-19 14:28:36 +01:00
set -- -c listen_addresses='*' -p "${PGPORT:-5432}"
2026-02-19 14:15:02 +01:00
NOTIFY_SOCKET= \
PGUSER="${PGUSER:-$POSTGRES_USER}" \
pg_ctl -D "$PGDATA" \
2026-02-19 14:28:36 +01:00
-o "-c listen_addresses='*' -p ${PGPORT:-5432}" \
2026-02-19 14:15:02 +01:00
-w start
2026-02-19 13:55:38 +01:00
/usr/lib/postgresql/15/bin/repmgr -f /etc/repmgr.conf primary register
2026-02-19 11:09:35 +01:00
else
# this is a standby
docker_temp_server_stop
rm -rf /var/lib/postgresql/data/*
2026-02-19 13:55:38 +01:00
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
2026-02-19 14:00:40 +01:00
echo "pg_bindir = '/usr/lib/postgresql/15/bin/'" >> /etc/repmgr.conf
2026-02-19 11:09:35 +01:00
2026-02-19 14:18:28 +01:00
until /usr/lib/postgresql/15/bin/repmgr -h pg-0 -U repmgr -f /etc/repmgr.conf standby clone --dry-run &> /dev/null
2026-02-19 13:55:38 +01:00
do
echo "Upstream host not ready..."
sleep 300
done
2026-02-19 13:16:08 +01:00
/usr/lib/postgresql/15/bin/repmgr -h pg-0 -U repmgr -f /etc/repmgr.conf standby clone
2026-02-19 14:43:58 +01:00
/usr/lib/postgresql/15/bin/repmgr -f /etc/repmgr.conf standby register
2026-02-19 13:16:08 +01:00
2026-02-19 13:55:38 +01:00
docker_temp_server_start
2026-02-19 11:09:35 +01:00
fi