1preCheckHooks+=('postgresqlStart')
2postCheckHooks+=('postgresqlStop')
3
4
5postgresqlStart() {
6
7 # Add default environment variable values
8 #
9 # Client variables:
10 # - https://www.postgresql.org/docs/current/libpq-envars.html
11 #
12 # Server variables:
13 # - only PGDATA: https://www.postgresql.org/docs/current/creating-cluster.html
14
15 if [[ "${PGDATA:-}" == "" ]]; then
16 PGDATA="$NIX_BUILD_TOP/postgresql"
17 fi
18 export PGDATA
19
20 if [[ "${PGHOST:-}" == "" ]]; then
21 mkdir -p "$NIX_BUILD_TOP/run/postgresql"
22 PGHOST="$NIX_BUILD_TOP/run/postgresql"
23 fi
24 export PGHOST
25
26 if [[ "${PGUSER:-}" == "" ]]; then
27 PGUSER="test_user"
28 fi
29 export PGUSER
30
31 if [[ "${PGDATABASE:-}" == "" ]]; then
32 PGDATABASE="test_db"
33 fi
34 export PGDATABASE
35
36 if [[ "${postgresqlTestUserOptions:-}" == "" ]]; then
37 postgresqlTestUserOptions="LOGIN"
38 fi
39
40 if [[ "${postgresqlTestSetupSQL:-}" == "" ]]; then
41 postgresqlTestSetupSQL="$(cat <<EOF
42 CREATE ROLE "$PGUSER" $postgresqlTestUserOptions;
43 CREATE DATABASE "$PGDATABASE" OWNER '$PGUSER';
44EOF
45 )"
46 fi
47
48 if [[ "${postgresqlTestSetupCommands:-}" == "" ]]; then
49 postgresqlTestSetupCommands='echo "$postgresqlTestSetupSQL" | PGUSER=postgres psql postgres'
50 fi
51
52 if ! type initdb >/dev/null; then
53 echo >&2 'initdb not found. Did you add postgresql to the nativeCheckInputs?'
54 false
55 fi
56 echo 'initializing postgresql'
57 initdb -U postgres
58
59 echo "$postgresqlExtraSettings" >>"$PGDATA/postgresql.conf"
60
61 # Move the socket
62 echo "unix_socket_directories = '$NIX_BUILD_TOP/run/postgresql'" >>"$PGDATA/postgresql.conf"
63
64 # TCP ports can be a problem in some sandboxes,
65 # so we disable tcp listening by default
66 if ! [[ "${postgresqlEnableTCP:-}" = 1 ]]; then
67 echo "listen_addresses = ''" >>"$PGDATA/postgresql.conf"
68 fi
69
70 echo 'starting postgresql'
71 eval "${postgresqlStartCommands:-pg_ctl start}"
72
73 echo 'setting up postgresql'
74 eval "$postgresqlTestSetupCommands"
75
76 runHook postgresqlTestSetupPost
77
78}
79
80postgresqlStop() {
81 echo 'stopping postgresql'
82 pg_ctl stop
83}