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
2023-08-20 21:27:11 +00:00
create_dir " ${ EXTRA_CONF_DIR } "
create_dir " ${ CONF_LOCKFILE_DIR } "
create_dir " ${ SCRIPTS_LOCKFILE_DIR } "
2022-04-29 14:05:31 +00:00
SETUP_LOCKFILE = " ${ CONF_LOCKFILE_DIR } /.postgresql.conf.lock "
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.
2023-08-20 21:27:11 +00:00
cat " ${ CONF } " .template > " ${ CONF } "
2018-03-21 20:53:39 +00:00
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
2023-08-20 21:27:11 +00:00
sed -i '/data_directory/d' " ${ CONF } "
2020-04-08 08:15:45 +00:00
2021-01-05 09:32:26 +00:00
# Create a config to optimise postgis
2023-08-20 21:27:11 +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 }
2024-03-17 18:07:08 +00:00
maintenance_work_mem = ${ MAINTENANCE_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'
2024-03-17 18:07:08 +00:00
max_parallel_maintenance_workers = ${ MAINTENANCE_WORKERS }
2019-08-23 13:43:58 +00:00
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
2023-08-20 21:27:11 +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
2023-08-20 21:27:11 +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
2023-08-20 21:27:11 +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
2022-05-09 16:12:01 +00:00
postgres_ssl_setup
2023-08-20 21:27:11 +00:00
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 }
hot_standby = on
checkpoint_timeout = ${ CHECK_POINT_TIMEOUT }
2022-05-09 16:12:01 +00:00
primary_conninfo = 'host=${REPLICATE_FROM} port=${REPLICATE_PORT} user=${REPLICATION_USER} password=${REPLICATION_PASS} ${PARAMS}'
2020-10-01 15:55:53 +00:00
recovery_target_timeline = ${ TARGET_TIMELINE }
recovery_target_action = ${ TARGET_ACTION }
EOF
2022-05-07 13:08:57 +00:00
if [ [ ${ ARCHIVE_MODE } = ~ [ Oo] [ Nn] ] ] ; then
2023-08-20 21:27:11 +00:00
cat >> " ${ ROOT_CONF } " /streaming_replication.conf <<EOF
2022-05-07 13:08:57 +00:00
archive_mode = ${ ARCHIVE_MODE }
archive_command = '${ARCHIVE_COMMAND}'
archive_cleanup_command = '${ARCHIVE_CLEANUP_COMMAND}'
EOF
fi
2023-08-20 21:27:11 +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
2022-04-29 14:05:31 +00:00
# If it doesn't exists, copy from ${EXTRA_CONF_DIR} directory if exists
if [ [ -f ${ EXTRA_CONF_DIR } /extra.conf ] ] ; then
2023-08-20 21:27:11 +00:00
cp -f " ${ EXTRA_CONF_DIR } " /extra.conf " ${ ROOT_CONF } " /extra.conf
echo "include 'extra.conf'" >> " ${ CONF } "
2021-03-01 06:42:38 +00:00
else
# default value
2021-03-02 05:45:59 +00:00
if [ [ -n " $EXTRA_CONF " ] ] ; then
2023-08-20 21:27:11 +00:00
echo -e " ${ EXTRA_CONF } " >> " ${ ROOT_CONF } " /extra.conf
echo "include 'extra.conf'" >> " ${ CONF } "
2021-03-01 06:42:38 +00:00
fi
fi
2021-01-05 13:46:47 +00:00
fi
2019-10-25 12:43:15 +00:00
2023-03-25 09:13:07 +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)
2022-02-11 10:03:44 +00:00
if [ [ $( dpkg -l | grep "timescaledb" ) > /dev/null ] ] && [ [ ${ ACCEPT_TIMESCALE_TUNING } = ~ [ Tt] [ Rr] [ Uu] [ Ee] ] ] ; then
2023-03-25 09:13:07 +00:00
# copy default conf as a backup
cp " ${ ROOT_CONF } " /postgresql.conf " ${ ROOT_CONF } " /postgresql_orig.conf
2022-10-26 05:39:14 +00:00
over_write_conf
2021-12-03 17:08:00 +00:00
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
2023-08-20 21:27:11 +00:00
timescaledb-tune -yes -quiet " ${ TIMESCALE_TUNING_PARAMS } " --dry-run >" ${ ROOT_CONF } " /" ${ TIMESCALE_TUNING_CONFIG } "
2023-03-25 09:13:07 +00:00
if [ [ -f " ${ ROOT_CONF } " /${ TIMESCALE_TUNING_CONFIG } ] ] ; then
2023-08-20 21:27:11 +00:00
mv " ${ ROOT_CONF } " /postgresql_orig.conf " ${ CONF } "
echo " include ' ${ TIMESCALE_TUNING_CONFIG } ' " >> " ${ CONF } "
2023-03-25 09:13:07 +00:00
fi
echo -e " \033[0m Time scale config tuning values set in ${ ROOT_CONF } / ${ TIMESCALE_TUNING_CONFIG } "
2021-12-03 17:08:00 +00:00
fi
2022-10-26 05:39:14 +00:00
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
2023-09-04 19:56:45 +00:00
echo " kernel.shmmax= ${ KERNEL_SHMMAX } " >> /etc/sysctl.conf
echo " kernel.shmall= ${ KERNEL_SHMALL } " >> /etc/sysctl.conf
2019-01-25 12:57:23 +00:00
2019-02-01 14:16:59 +00:00
# Put lock file to make sure conf was not reinitialized
2023-08-20 21:27:11 +00:00
touch " ${ SETUP_LOCKFILE } "