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

80 lines
3.7 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 14:53:12 +01:00
server_start() {
NOTIFY_SOCKET= \
PGUSER="${PGUSER:-$POSTGRES_USER}" \
pg_ctl -D "$PGDATA" \
-o "-c listen_addresses='*' -p ${PGPORT:-5432}" \
-w start
}
2026-02-20 13:38:19 +01:00
create_repmgr() {
psql -c "CREATE USER repmgr WITH SUPERUSER ENCRYPTED PASSWORD '${REPMGR_PASSWORD}';"
psql -c "CREATE DATABASE repmgr WITH OWNER repmgr;"
psql -c 'ALTER USER repmgr SET search_path TO repmgr, "$user", public;'
}
create_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
2026-02-20 13:43:24 +01:00
echo "host replication repmgr ${POSTGRES_NETWORK:-100.64.0.0/10} trust" >> /var/lib/postgresql/data/pg_hba.conf
2026-02-20 13:38:19 +01:00
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
2026-02-20 13:43:24 +01:00
echo "host repmgr repmgr ${POSTGRES_NETWORK:-100.64.0.0/10} trust" >> /var/lib/postgresql/data/pg_hba.conf
2026-02-20 13:38:19 +01:00
echo "host all all all scram-sha-256" >> /var/lib/postgresql/data/pg_hba.conf
}
2026-02-20 11:06:41 +01:00
echo "node_id = ${REPMGR_NODE_ID}" >> /etc/repmgr.conf
echo "node_name = ${REPMGR_NODE_NAME}" >> /etc/repmgr.conf
echo "conninfo = 'host=${REPMGR_NODE_NAME} dbname=repmgr user=repmgr connect_timeout=2'" >> /etc/repmgr.conf
echo "location = '${REPMGR_NODE_LOCATION}'" >> /etc/repmgr.conf
2026-02-19 11:09:35 +01:00
2026-02-20 11:33:11 +01:00
if [[ -n $REPMGR_UPSTREAM ]] && [[ -z $REPMGR_NODE_ROLE ]]; then
2026-02-20 11:12:59 +01:00
REPMGR_NODE_ROLE="standby"
2026-02-19 11:09:35 +01:00
fi
2026-02-20 11:06:41 +01:00
2026-02-20 11:12:59 +01:00
case "$REPMGR_NODE_ROLE" in
2026-02-20 11:06:41 +01:00
standby )
docker_temp_server_stop
rm -rf /var/lib/postgresql/data/*
until /usr/lib/postgresql/15/bin/repmgr -h $REPMGR_UPSTREAM -U repmgr -f /etc/repmgr.conf standby clone --dry-run &> /dev/null
do
echo "Upstream host not ready. Waiting for 5 minutes..."
sleep 300
done
echo "Upstream host found..."
/usr/lib/postgresql/15/bin/repmgr -h $REPMGR_UPSTREAM -U repmgr -f /etc/repmgr.conf standby clone
server_start
/usr/lib/postgresql/15/bin/repmgr -f /etc/repmgr.conf standby register
;;
witness )
2026-02-20 13:38:19 +01:00
create_repmgr
2026-02-20 13:32:47 +01:00
docker_temp_server_stop
2026-02-20 13:38:19 +01:00
create_hba
2026-02-20 13:32:47 +01:00
server_start
2026-02-20 11:06:41 +01:00
until /usr/lib/postgresql/15/bin/repmgr -h $REPMGR_UPSTREAM -f /etc/repmgr.conf witness register &> /dev/null
do
echo "Primary host not ready. Waiting for 5 minutes..."
sleep 300
done
;;
* )
2026-02-20 13:38:19 +01:00
create_repmgr
2026-02-20 11:06:41 +01:00
docker_temp_server_stop
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 "max_wal_senders = 10" >> /var/lib/postgresql/data/postgresql.conf
echo "max_replication_slots = 10" >> /var/lib/postgresql/data/postgresql.conf
if [[ -n $POSTGRES_MAX_CONNECTIONS ]]; then
echo "max_connections = ${POSTGRES_MAX_CONNECTIONS}" >> /var/lib/postgresql/data/postgresql.conf
fi
2026-02-20 13:38:19 +01:00
create_hba
2026-02-20 11:06:41 +01:00
server_start
/usr/lib/postgresql/15/bin/repmgr -f /etc/repmgr.conf primary register
;;
esac