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 "
2021-06-10 16:27:48 +00:00
create_dir /settings
2019-02-01 14:16:59 +00:00
if [ -f " ${ SETUP_LOCKFILE } " ] ; then
return 0
fi
2018-03-21 20:53:39 +00:00
# Refresh configuration in case environment settings changed.
cat $CONF .template > $CONF
2021-01-05 17:09:48 +00:00
# Reflect DATA DIR location
2020-04-08 08:15:45 +00:00
# Delete any data_dir declarations
sed -i '/data_directory/d' $CONF
2021-01-05 09:32:26 +00:00
# Create a config to optimise postgis
2021-03-01 06:42:38 +00:00
cat > ${ ROOT_CONF } /postgis.conf <<EOF
2021-01-05 17:09:48 +00:00
data_directory = '${DATADIR}'
2021-01-03 11:18:32 +00:00
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
2021-01-05 09:32:26 +00:00
# Create a config for logical replication
2020-10-01 15:55:53 +00:00
if [ [ " ${ REPLICATION } " = ~ [ Tt] [ Rr] [ Uu] [ Ee] && " $WAL_LEVEL " = = 'logical' ] ] ; then
2021-03-01 06:42:38 +00:00
cat > ${ ROOT_CONF } /logical_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
2021-01-05 09:32:26 +00:00
# Create a config for streaming replication
2020-10-01 15:55:53 +00:00
if [ [ " ${ REPLICATION } " = ~ [ Tt] [ Rr] [ Uu] [ Ee] && " $WAL_LEVEL " = = 'replica' ] ] ; then
2021-03-01 06:42:38 +00:00
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
2021-03-01 06:42:38 +00:00
if [ [ ! -f ${ ROOT_CONF } /extra.conf ] ] ; then
# If it doesn't exists, copy from /settings directory if exists
if [ [ -f /settings/extra.conf ] ] ; then
cp -f /settings/extra.conf ${ ROOT_CONF } /extra.conf
echo "include 'extra.conf'" >> $CONF
else
# default value
2021-03-02 05:45:59 +00:00
if [ [ -n " $EXTRA_CONF " ] ] ; then
2021-03-01 06:42:38 +00:00
echo -e $EXTRA_CONF >> ${ ROOT_CONF } /extra.conf
echo "include 'extra.conf'" >> $CONF
fi
fi
2021-01-05 13:46:47 +00:00
fi
2019-10-25 12:43:15 +00:00
2021-12-03 17:08:00 +00:00
# Timescale default tuning
# TODO If timescale DB accepts reading from include directory then refactor code to remove line 97 - 112 (https://github.com/timescale/timescaledb-tune/issues/80)
if [ [ ${ ACCEPT_TIMESCALE_TUNING } = ~ [ Tt] [ Rr] [ Uu] [ Ee] ] ] ; then
if [ [ -f ${ ROOT_CONF } /postgis.conf ] ] ; then
sed -i '/postgis.conf/d' " ${ ROOT_CONF } " /postgresql.conf
cat " ${ ROOT_CONF } " /postgis.conf >> " ${ ROOT_CONF } " /postgresql.conf
fi
if [ [ -f ${ ROOT_CONF } /logical_replication.conf ] ] ; then
sed -i '/logical_replication.conf/d' " ${ ROOT_CONF } " /postgresql.conf
cat " ${ ROOT_CONF } " /logical_replication.conf >> " ${ ROOT_CONF } " /postgresql.conf
fi
if [ [ -f ${ ROOT_CONF } /streaming_replication.conf ] ] ; then
sed -i '/streaming_replication.conf/d' " ${ ROOT_CONF } " /postgresql.conf
cat " ${ ROOT_CONF } " /streaming_replication.conf >> " ${ ROOT_CONF } " /postgresql.conf
fi
if [ [ -f ${ ROOT_CONF } /extra.conf ] ] ; then
sed -i '/extra.conf/d' " ${ ROOT_CONF } " /postgresql.conf
cat " ${ ROOT_CONF } " /extra.conf >> " ${ ROOT_CONF } " /postgresql.conf
fi
echo -e "\e[1;31m Time scale config tuning values below"
# TODO Add logic to find defaults memory, CPUS as these can vary from defaults on host machine and in docker container
timescaledb-tune -yes -quiet " ${ TIMESCALE_TUNING_PARAMS } " --conf-path= " ${ ROOT_CONF } " /postgresql.conf
echo -e " \033[0m Time scale config tuning values set in ${ ROOT_CONF } /postgresql.conf "
fi
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 }