add option for witness

This commit is contained in:
Bernhard Radermacher
2026-02-20 11:06:41 +01:00
parent 260423fc3a
commit 5824741ef4
3 changed files with 58 additions and 70 deletions

View File

@@ -6,8 +6,7 @@ RUN apt-get update && \
&& \ && \
rm -rf /var/lib/apt/lists/* rm -rf /var/lib/apt/lists/*
RUN touch /etc/repmgr.conf && chmod 666 /etc/repmgr.conf COPY --chmod=666 repmgr.conf /etc/repmgr.conf
COPY --chmod=644 --chown=postgres profile /var/lib/postgresql/.profile
COPY profile /var/lib/postgresql/.profile
COPY docker-entrypoint-initdb.d/* /docker-entrypoint-initdb.d/ COPY docker-entrypoint-initdb.d/* /docker-entrypoint-initdb.d/

View File

@@ -10,72 +10,58 @@ server_start() {
-w start -w start
} }
# the variable UPSTREAM indicates that a standby instance is requested, otherwise this is primary echo "node_id = ${REPMGR_NODE_ID}" >> /etc/repmgr.conf
if [[ -z $REPMGR_UPSTREAM ]]; then 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
psql -c "CREATE USER repmgr WITH SUPERUSER ENCRYPTED PASSWORD '${REPMGR_PASSWORD}';" echo "location = '${REPMGR_NODE_LOCATION}'" >> /etc/repmgr.conf
psql -c "CREATE DATABASE repmgr WITH OWNER repmgr;"
psql -c 'ALTER USER repmgr SET search_path TO repmgr, "$user", public;'
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
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 ${POSTGRES_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 127.0.0.1/32 trust" >> /var/lib/postgresql/data/pg_hba.conf
echo "host repmgr repmgr ${POSTGRES_NETWORK-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 "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
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
# process selected tweaks
if [[ -n $POSTGRES_MAX_CONNECTIONS ]]; then
echo "max_connections = ${POSTGRES_MAX_CONNECTIONS}" >> /var/lib/postgresql/data/postgresql.conf
fi
server_start
/usr/lib/postgresql/15/bin/repmgr -f /etc/repmgr.conf primary register
else
# this is a standby
docker_temp_server_stop
rm -rf /var/lib/postgresql/data/*
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
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
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
if [[ -n $REPMGR_UPSTREAM ]] && [[ -z $REPMGR_ROLE ]]; then
REPMGR_ROLE="standby"
fi fi
case "$REPMGR_ROLE" in
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 )
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
;;
* )
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;'
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
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 ${POSTGRES_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 127.0.0.1/32 trust" >> /var/lib/postgresql/data/pg_hba.conf
echo "host repmgr repmgr ${POSTGRES_NETWORK-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
server_start
/usr/lib/postgresql/15/bin/repmgr -f /etc/repmgr.conf primary register
;;
esac

3
repmgr.conf Normal file
View File

@@ -0,0 +1,3 @@
data_directory = '/var/lib/postgresql/data'
use_replication_slots = on
pg_bindir = '/usr/lib/postgresql/15/bin/'