2022-04-16 15:17:46 +00:00
|
|
|
preCheckHooks+=('postgresqlStart')
|
|
|
|
postCheckHooks+=('postgresqlStop')
|
|
|
|
|
|
|
|
|
|
|
|
postgresqlStart() {
|
|
|
|
|
|
|
|
# Add default environment variable values
|
|
|
|
#
|
|
|
|
# Client variables:
|
|
|
|
# - https://www.postgresql.org/docs/current/libpq-envars.html
|
|
|
|
#
|
|
|
|
# Server variables:
|
|
|
|
# - only PGDATA: https://www.postgresql.org/docs/current/creating-cluster.html
|
|
|
|
|
|
|
|
if [[ "${PGDATA:-}" == "" ]]; then
|
|
|
|
PGDATA="$NIX_BUILD_TOP/postgresql"
|
|
|
|
fi
|
|
|
|
export PGDATA
|
|
|
|
|
|
|
|
if [[ "${PGHOST:-}" == "" ]]; then
|
|
|
|
mkdir -p "$NIX_BUILD_TOP/run/postgresql"
|
|
|
|
PGHOST="$NIX_BUILD_TOP/run/postgresql"
|
|
|
|
fi
|
|
|
|
export PGHOST
|
|
|
|
|
|
|
|
if [[ "${PGUSER:-}" == "" ]]; then
|
|
|
|
PGUSER="test_user"
|
|
|
|
fi
|
|
|
|
export PGUSER
|
|
|
|
|
|
|
|
if [[ "${PGDATABASE:-}" == "" ]]; then
|
|
|
|
PGDATABASE="test_db"
|
|
|
|
fi
|
|
|
|
export PGDATABASE
|
|
|
|
|
|
|
|
if [[ "${postgresqlTestUserOptions:-}" == "" ]]; then
|
|
|
|
postgresqlTestUserOptions="LOGIN"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ "${postgresqlTestSetupSQL:-}" == "" ]]; then
|
|
|
|
postgresqlTestSetupSQL="$(cat <<EOF
|
|
|
|
CREATE ROLE "$PGUSER" $postgresqlTestUserOptions;
|
|
|
|
CREATE DATABASE "$PGDATABASE" OWNER '$PGUSER';
|
|
|
|
EOF
|
|
|
|
)"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ "${postgresqlTestSetupCommands:-}" == "" ]]; then
|
|
|
|
postgresqlTestSetupCommands='echo "$postgresqlTestSetupSQL" | PGUSER=postgres psql postgres'
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! type initdb >/dev/null; then
|
2023-01-20 12:00:00 +00:00
|
|
|
echo >&2 'initdb not found. Did you add postgresql to the nativeCheckInputs?'
|
2022-04-16 15:17:46 +00:00
|
|
|
false
|
|
|
|
fi
|
2023-01-15 22:08:12 +00:00
|
|
|
echo 'initializing postgresql'
|
2022-04-16 15:17:46 +00:00
|
|
|
initdb -U postgres
|
|
|
|
|
2024-01-22 04:20:00 +00:00
|
|
|
echo "$postgresqlExtraSettings" >>"$PGDATA/postgresql.conf"
|
|
|
|
|
2022-04-16 15:17:46 +00:00
|
|
|
# Move the socket
|
|
|
|
echo "unix_socket_directories = '$NIX_BUILD_TOP/run/postgresql'" >>"$PGDATA/postgresql.conf"
|
|
|
|
|
|
|
|
# TCP ports can be a problem in some sandboxes,
|
|
|
|
# so we disable tcp listening by default
|
|
|
|
if ! [[ "${postgresqlEnableTCP:-}" = 1 ]]; then
|
|
|
|
echo "listen_addresses = ''" >>"$PGDATA/postgresql.conf"
|
|
|
|
fi
|
|
|
|
|
2023-01-15 22:08:12 +00:00
|
|
|
echo 'starting postgresql'
|
2022-04-16 15:17:46 +00:00
|
|
|
eval "${postgresqlStartCommands:-pg_ctl start}"
|
|
|
|
|
2023-01-15 22:08:12 +00:00
|
|
|
echo 'setting up postgresql'
|
2022-04-16 15:17:46 +00:00
|
|
|
eval "$postgresqlTestSetupCommands"
|
|
|
|
|
2023-01-25 08:40:14 +00:00
|
|
|
runHook postgresqlTestSetupPost
|
|
|
|
|
2022-04-16 15:17:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
postgresqlStop() {
|
2023-01-15 22:08:12 +00:00
|
|
|
echo 'stopping postgresql'
|
2022-04-16 15:17:46 +00:00
|
|
|
pg_ctl stop
|
|
|
|
}
|