2018-03-21 20:53:39 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2020-04-14 17:00:36 +00:00
|
|
|
source /scripts/env-data.sh
|
2018-03-21 20:53:39 +00:00
|
|
|
|
2019-02-01 14:16:59 +00:00
|
|
|
SETUP_LOCKFILE="${ROOT_CONF}/.postgresql.conf.lock"
|
|
|
|
if [ -f "${SETUP_LOCKFILE}" ]; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
2018-03-21 20:53:39 +00:00
|
|
|
# This script will setup necessary configuration to enable replications
|
|
|
|
|
|
|
|
# Refresh configuration in case environment settings changed.
|
|
|
|
cat $CONF.template > $CONF
|
|
|
|
|
2020-04-08 08:15:45 +00:00
|
|
|
# Reflect DATADIR loaction
|
|
|
|
# Delete any data_dir declarations
|
|
|
|
sed -i '/data_directory/d' $CONF
|
|
|
|
echo "data_directory = '${DATADIR}'" >> $CONF
|
|
|
|
|
2019-01-25 12:57:23 +00:00
|
|
|
# This script will setup necessary configuration to optimise for PostGIS and to enable replications
|
2021-01-03 11:18:32 +00:00
|
|
|
if [[ -f ${ROOT_CONF}/postgis.conf ]];then
|
|
|
|
rm $CONF/postgis.conf
|
|
|
|
fi
|
|
|
|
cat >> ${ROOT_CONF}/postgis.conf <<EOF
|
|
|
|
port = 5432
|
2019-02-20 10:28:14 +00:00
|
|
|
superuser_reserved_connections= 10
|
2019-01-25 12:57:23 +00:00
|
|
|
listen_addresses = '${IP_LIST}'
|
2020-10-09 06:09:56 +00:00
|
|
|
shared_buffers = ${SHARED_BUFFERS}
|
|
|
|
work_mem = ${WORK_MEM}
|
2019-09-06 11:47:42 +00:00
|
|
|
maintenance_work_mem = ${MAINTAINANCE_WORK_MEM}
|
2020-10-09 06:09:56 +00:00
|
|
|
wal_buffers = ${WAL_BUFFERS}
|
2019-01-25 12:57:23 +00:00
|
|
|
random_page_cost = 2.0
|
|
|
|
xmloption = 'document'
|
2019-08-23 13:43:58 +00:00
|
|
|
max_parallel_maintenance_workers = ${MAINTAINANCE_WORKERS}
|
|
|
|
max_parallel_workers = ${MAX_WORKERS}
|
2020-04-14 17:00:36 +00:00
|
|
|
shared_preload_libraries = '${SHARED_PRELOAD_LIBRARIES}'
|
2020-01-25 15:06:05 +00:00
|
|
|
cron.database_name = '${SINGLE_DB}'
|
2020-05-01 05:33:49 +00:00
|
|
|
password_encryption= '${PASSWORD_AUTHENTICATION}'
|
2020-06-14 16:22:13 +00:00
|
|
|
timezone='${TIMEZONE}'
|
2020-11-04 19:25:18 +00:00
|
|
|
cron.use_background_workers = on
|
2018-03-21 20:53:39 +00:00
|
|
|
EOF
|
2019-01-25 12:57:23 +00:00
|
|
|
|
2021-01-03 11:18:32 +00:00
|
|
|
echo "include 'postgis.conf'" >> $CONF
|
2020-10-01 15:55:53 +00:00
|
|
|
# This script will setup necessary replication settings
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if [[ "${REPLICATION}" =~ [Tt][Rr][Uu][Ee] && "$WAL_LEVEL" == 'logical' ]]; then
|
2021-01-03 11:18:32 +00:00
|
|
|
if [[ -f ${ROOT_CONF}/logical_replication.conf ]];then
|
|
|
|
rm $CONF/logical_replication.conf
|
|
|
|
fi
|
|
|
|
cat >> ${ROOT_CONF}/streaming_replication.conf <<EOF
|
2020-10-01 15:55:53 +00:00
|
|
|
wal_level = ${WAL_LEVEL}
|
|
|
|
max_wal_senders = ${PG_MAX_WAL_SENDERS}
|
2020-10-09 06:09:56 +00:00
|
|
|
wal_keep_size = ${PG_WAL_KEEP_SIZE}
|
2020-10-01 15:55:53 +00:00
|
|
|
min_wal_size = ${MIN_WAL_SIZE}
|
|
|
|
max_wal_size = ${WAL_SIZE}
|
|
|
|
max_logical_replication_workers = ${MAX_LOGICAL_REPLICATION_WORKERS}
|
|
|
|
max_sync_workers_per_subscription = ${MAX_SYNC_WORKERS_PER_SUBSCRIPTION}
|
|
|
|
EOF
|
2021-01-03 11:18:32 +00:00
|
|
|
echo "include 'logical_replication.conf'" >> $CONF
|
2020-10-01 15:55:53 +00:00
|
|
|
fi
|
2019-11-25 12:47:29 +00:00
|
|
|
|
2020-10-01 15:55:53 +00:00
|
|
|
if [[ "${REPLICATION}" =~ [Tt][Rr][Uu][Ee] && "$WAL_LEVEL" == 'replica' ]]; then
|
2021-01-03 11:18:32 +00:00
|
|
|
if [[ -f ${ROOT_CONF}/streaming_replication.conf ]];then
|
|
|
|
rm $CONF/streaming_replication.conf
|
|
|
|
fi
|
|
|
|
cat >> ${ROOT_CONF}/streaming_replication.conf <<EOF
|
2020-10-01 15:55:53 +00:00
|
|
|
wal_level = ${WAL_LEVEL}
|
|
|
|
archive_mode = ${ARCHIVE_MODE}
|
|
|
|
archive_command = '${ARCHIVE_COMMAND}'
|
|
|
|
restore_command = '${RESTORE_COMMAND}'
|
|
|
|
archive_cleanup_command = '${ARCHIVE_CLEANUP_COMMAND}'
|
|
|
|
max_wal_senders = ${PG_MAX_WAL_SENDERS}
|
2020-10-09 06:09:56 +00:00
|
|
|
wal_keep_size = ${PG_WAL_KEEP_SIZE}
|
2020-10-01 15:55:53 +00:00
|
|
|
min_wal_size = ${MIN_WAL_SIZE}
|
|
|
|
max_wal_size = ${WAL_SIZE}
|
|
|
|
hot_standby = on
|
|
|
|
checkpoint_timeout = ${CHECK_POINT_TIMEOUT}
|
|
|
|
primary_conninfo = 'host=${REPLICATE_FROM} port=${REPLICATE_PORT} user=${REPLICATION_USER} password=${REPLICATION_PASS} sslmode=${PGSSLMODE}'
|
|
|
|
recovery_target_timeline=${TARGET_TIMELINE}
|
|
|
|
recovery_target_action=${TARGET_ACTION}
|
|
|
|
promote_trigger_file = '${PROMOTE_FILE}'
|
|
|
|
EOF
|
2021-01-03 11:18:32 +00:00
|
|
|
echo "include 'streaming_replication.conf'" >> $CONF
|
2020-10-01 15:55:53 +00:00
|
|
|
fi
|
2019-11-25 12:47:29 +00:00
|
|
|
|
2019-10-25 12:43:15 +00:00
|
|
|
echo -e $EXTRA_CONF >> $CONF
|
|
|
|
|
2019-01-25 12:57:23 +00:00
|
|
|
# Optimise PostgreSQL shared memory for PostGIS
|
|
|
|
# shmall units are pages and shmmax units are bytes(?) equivalent to the desired shared_buffer size set in setup_conf.sh - in this case 500MB
|
|
|
|
echo "kernel.shmmax=543252480" >> /etc/sysctl.conf
|
|
|
|
echo "kernel.shmall=2097152" >> /etc/sysctl.conf
|
|
|
|
|
2019-02-01 14:16:59 +00:00
|
|
|
# Put lock file to make sure conf was not reinitialized
|
|
|
|
touch ${SETUP_LOCKFILE}
|