Files
postgres15replicate/docker-entrypoint-initdb.d/00-main.sh
Bernhard Radermacher 072b7fef92 initial
2026-02-11 12:53:58 +00:00

39 lines
1.6 KiB
Bash
Executable File

#!/usr/bin/env bash
# the variable UPSTREAM indicates that a standby instance is requested, otherwise this is primary
if [[ -z $UPSTREAM ]]; then
# 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_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 "wal_level = replica" >> /var/lib/postgresql/data/postgresql.conf
# ensure required entries in hba
echo "host replication ${REPLICATOR_NAME:-replicator} all md5" >> /var/lib/postgresql/data/pg_hba.conf
# create user for replication
psql -c "CREATE USER ${REPLICATOR_NAME:-replicator} WITH REPLICATION ENCRYPTED PASSWORD '${REPLICATOR_PASSWORD}'; GRANT CONNECT ON DATABASE postgres TO ${REPLICATOR_NAME:-replicator}"
# process selected tweaks
if [[ -n $MAX_CONNECTIONS ]]; then
echo "max_connections = ${MAX_CONNECTIONS}" >> /var/lib/postgresql/data/postgresql.conf
fi
else
# this is a standby
source /usr/local/bin/docker-entrypoint.sh
docker_temp_server_stop
rm -rf /var/lib/postgresql/data/*
OLD_PGPASSWORD=$PGPASSWORD
export PGPASSWORD=$REPLICATOR_PASSWORD
pg_basebackup -h "${UPSTREAM}" -U "${REPLICATOR_NAME:-replicator}" -D "${PGDATA}" -Fp -Xs -P -R
export PGPASSWORD=$OLD_PGPASSWORD
fi