2026-02-23 14:14:06 +01:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
|
|
set -Eeo pipefail
|
|
|
|
|
|
2026-02-27 10:48:28 +01:00
|
|
|
file_env() {
|
|
|
|
|
local var="$1"
|
|
|
|
|
local fileVar="${var}_FILE"
|
|
|
|
|
local def="${2:-}"
|
|
|
|
|
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
|
|
|
|
|
printf >&2 'error: both %s and %s are set (but are exclusive)\n' "$var" "$fileVar"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
local val="$def"
|
|
|
|
|
if [ "${!var:-}" ]; then
|
|
|
|
|
val="${!var}"
|
|
|
|
|
elif [ "${!fileVar:-}" ]; then
|
|
|
|
|
val="$(< "${!fileVar}")"
|
|
|
|
|
fi
|
|
|
|
|
export "$var"="$val"
|
|
|
|
|
unset "$fileVar"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
docker_setup_env() {
|
|
|
|
|
file_env 'PGPOOL_ADMIN_USERNAME'
|
|
|
|
|
file_env 'PGPOOL_ADMIN_PASSWORD'
|
|
|
|
|
file_env 'POSTGRES_PGPOOL_USERNAME'
|
|
|
|
|
file_env 'POSTGRES_PGPOOL_PASSWORD'
|
2026-03-12 13:50:58 +01:00
|
|
|
file_env 'POSTGRES_REPLICATOR_USERNAME'
|
|
|
|
|
file_env 'POSTGRES_REPLICATOR_PASSWORD'
|
2026-02-27 10:48:28 +01:00
|
|
|
}
|
|
|
|
|
|
2026-02-27 11:31:29 +01:00
|
|
|
if [ "$(id -u)" = '0' ]; then
|
2026-02-27 14:09:18 +01:00
|
|
|
service ssh start
|
2026-02-27 11:34:50 +01:00
|
|
|
chown -R postgres /etc/pgpool2
|
2026-02-27 11:31:29 +01:00
|
|
|
exec gosu postgres "$BASH_SOURCE"
|
|
|
|
|
fi
|
|
|
|
|
|
2026-02-27 10:48:28 +01:00
|
|
|
install --directory --owner postgres --group postgres --mode 700 /var/lib/postgresql/.ssh
|
|
|
|
|
cp /ssh/* /var/lib/postgresql/.ssh/
|
2026-02-27 11:27:57 +01:00
|
|
|
chmod 600 /var/lib/postgresql/.ssh/*
|
2026-02-27 10:48:28 +01:00
|
|
|
|
|
|
|
|
docker_setup_env
|
2026-02-23 14:14:06 +01:00
|
|
|
|
2026-02-27 14:09:18 +01:00
|
|
|
{
|
|
|
|
|
printf "%s:" "$PGPOOL_ADMIN_USERNAME"
|
|
|
|
|
pg_md5 "$PGPOOL_ADMIN_PASSWORD"
|
|
|
|
|
printf "\n"
|
|
|
|
|
} > /etc/pgpool2/pcp.conf
|
|
|
|
|
{
|
|
|
|
|
printf "localhost:9898:%s:%s\n" "$PGPOOL_ADMIN_USERNAME" "$PGPOOL_ADMIN_PASSWORD"
|
|
|
|
|
printf "pgpool:9898:%s:%s\n" "$PGPOOL_ADMIN_USERNAME" "$PGPOOL_ADMIN_PASSWORD"
|
|
|
|
|
} > ~/.pcppass
|
|
|
|
|
chmod 600 /etc/pgpool2/pcp.conf ~/.pcppass
|
2026-03-12 13:50:58 +01:00
|
|
|
#{
|
|
|
|
|
# openssl rand -base64 33
|
|
|
|
|
#} > ~/.pgpoolkey
|
|
|
|
|
#chmod 600 ~/.pgpoolkey
|
|
|
|
|
# pg_enc -m -k ~/.pgpoolkey -u $POSTGRES_PGPOOL_USERNAME $POSTGRES_PGPOOL_PASSWORD
|
2026-02-27 13:32:02 +01:00
|
|
|
|
2026-02-27 14:09:18 +01:00
|
|
|
{
|
2026-03-17 12:32:45 +01:00
|
|
|
printf "# Backend Clustering Mode\n\n"
|
2026-03-12 13:50:58 +01:00
|
|
|
printf "backend_clustering_mode = 'streaming_replication'\n"
|
2026-02-27 14:09:18 +01:00
|
|
|
|
2026-03-17 12:32:45 +01:00
|
|
|
printf "\n# Connections\n\n"
|
|
|
|
|
printf "listen_addresses = '*'\n"
|
|
|
|
|
printf "port = 5432\n"
|
|
|
|
|
printf "unix_socket_directories = '/var/run/postgresql'\n"
|
|
|
|
|
printf "unix_socket_group = 'postgres'\n"
|
|
|
|
|
printf "unix_socket_permissions = 0777\n"
|
|
|
|
|
printf "pcp_port = 9898\n"
|
|
|
|
|
printf "pcp_socket_dir = '/var/run/postgresql'\n"
|
|
|
|
|
|
|
|
|
|
printf "\n# Backend Connections\n\n"
|
2026-02-27 14:09:18 +01:00
|
|
|
IFS=':'
|
|
|
|
|
n=0
|
|
|
|
|
for backend in $PGPOOL_BACKEND
|
|
|
|
|
do
|
2026-03-17 12:32:45 +01:00
|
|
|
printf "\nbackend_hostname%d = '%s'\n" $n $backend
|
|
|
|
|
printf "backend_port%d = 5432\n" $n
|
2026-03-12 15:57:05 +01:00
|
|
|
printf "backend_data_directory%d = '/var/lib/postgresql/data'\n" $n
|
2026-02-27 14:09:18 +01:00
|
|
|
((n+=1))
|
|
|
|
|
done
|
2026-03-17 12:32:45 +01:00
|
|
|
unset IFS
|
|
|
|
|
|
|
|
|
|
printf "\n# Authentication\n\n"
|
|
|
|
|
printf "enable_pool_hba = off\n"
|
|
|
|
|
printf "pool_passwd = ''\n"
|
|
|
|
|
printf "allow_clear_text_frontend_auth = on\n"
|
|
|
|
|
|
|
|
|
|
printf "\n# Pools\n\n"
|
|
|
|
|
printf "process_management_mode = 'dynamic'\n"
|
|
|
|
|
|
|
|
|
|
printf "\n# Logs\n\n"
|
|
|
|
|
printf "log_line_prefix = '%m: %a pid %p: '\n"
|
|
|
|
|
printf "log_per_node_statement = on\n"
|
|
|
|
|
printf "logging_collector = off\n"
|
|
|
|
|
|
|
|
|
|
printf "\n# Replication Mode\n\n"
|
|
|
|
|
|
|
|
|
|
printf "sr_check_period = 10\n"
|
|
|
|
|
printf "sr_check_user = %s\n" "$POSTGRES_REPLICATOR_USERNAME"
|
|
|
|
|
# printf "sr_check_password = %s\n" "$POSTGRES_REPLICATOR_PASSWORD"
|
|
|
|
|
printf "sr_check_password = ''\n"
|
|
|
|
|
printf "follow_primary_command = '/etc/pgpool2/follow_primary.sh %%d %%h %%p %%D %%m %%H %%M %%P %%r %%R'\n"
|
|
|
|
|
printf "replication_mode = off\n"
|
|
|
|
|
printf "master_slave_mode = on\n"
|
|
|
|
|
printf "load_balance_mode = on\n"
|
|
|
|
|
printf "reset_query_on_pool_release = on\n"
|
|
|
|
|
printf "replicate_on_reset = on\n"
|
|
|
|
|
printf "online_recovery = on\n"
|
|
|
|
|
printf "detach_false_primary = on\n"
|
|
|
|
|
printf "failover_command = '/etc/pgpool2/failover.sh %%d %%h %%p %%D %%m %%H %%M %%P %%r %%R %%N %%S'\n"
|
|
|
|
|
printf "recovery_user = postgres\n"
|
|
|
|
|
printf "recovery_password = ''\n"
|
|
|
|
|
printf "recovery_1st_stage_command = recovery_1st_stage\n"
|
|
|
|
|
printf "recovery_timeout = 60\n"
|
|
|
|
|
printf "client_idle_limit_in_recovery = -1\n"
|
|
|
|
|
printf "health_check_timeout = 5\n"
|
|
|
|
|
printf "health_check_period = 5\n"
|
|
|
|
|
printf "health_check_user = %s\n" "$POSTGRES_PGPOOL_USERNAME"
|
|
|
|
|
# printf "health_check_password = '%s'\n" "$PGPOOL_PASSWORD"
|
|
|
|
|
printf "health_check_password = ''\n"
|
|
|
|
|
printf "statement_cache_mode = off\n"
|
|
|
|
|
printf "query_cache_mode = off\n"
|
|
|
|
|
|
2026-02-27 14:09:18 +01:00
|
|
|
} > /etc/pgpool2/pgpool.conf
|
|
|
|
|
|
2026-03-12 15:57:05 +01:00
|
|
|
/usr/sbin/pgpool
|
2026-02-23 15:59:17 +01:00
|
|
|
|
2026-02-23 14:14:06 +01:00
|
|
|
while true
|
|
|
|
|
do
|
|
|
|
|
echo "$(date)"
|
|
|
|
|
sleep 3600
|
|
|
|
|
done
|