-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
pgutil.nix
55 lines (54 loc) · 1.7 KB
/
pgutil.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
{ writeScript, stdenv }:
# Change .pgdata location to $HOME/.pgdata to anticipate permission error
{
start_pg = writeScript "start-pg"
''
#!${stdenv.shell}
pg_pid=""
set -euo pipefail
# TODO: explain what's happening here
LOCAL_PGHOST=$PGHOST
LOCAL_PGPORT=$PGPORT
LOCAL_PGDATABASE=$PGDATABASE
LOCAL_PGUSER=$PGUSER
LOCAL_PGPASSWORD=$PGPASSWORD
unset PGUSER PGPASSWORD
# TODO: port
pg_ctl -D "$HOME/.pgdata" -w start || (echo pg_ctl failed; exit 1)
until psql postgres -c "SELECT 1" > /dev/null 2>&1 ; do
echo waiting for pg
sleep 0.5
done
'';
init_pg = writeScript "init-pg"
''
#!${stdenv.shell}
pg_pid=""
set -euo pipefail
# TODO: explain what's happening here
LOCAL_PGHOST=$PGHOST
LOCAL_PGPORT=$PGPORT
LOCAL_PGDATABASE=$PGDATABASE
LOCAL_PGUSER=$PGUSER
LOCAL_PGPASSWORD=$PGPASSWORD
unset PGUSER PGPASSWORD
initdb -D $HOME/.pgdata
echo "unix_socket_directories = '$(mktemp -d)'" >> $HOME/.pgdata/postgresql.conf
# TODO: port
pg_ctl -D "$HOME/.pgdata" -w start || (echo pg_ctl failed; exit 1)
until psql postgres -c "SELECT 1" > /dev/null 2>&1 ; do
echo waiting for pg
sleep 0.5
done
psql postgres -w -c "CREATE DATABASE $LOCAL_PGDATABASE"
psql postgres -w -c "CREATE ROLE $LOCAL_PGUSER WITH LOGIN PASSWORD '$LOCAL_PGPASSWORD'"
psql postgres -w -c "GRANT ALL PRIVILEGES ON DATABASE $LOCAL_PGDATABASE TO $LOCAL_PGUSER"
'';
stop_pg = writeScript "stop-pg"
''
#!${stdenv.shell}
pg_pid=""
set -euo pipefail
pg_ctl -D $HOME/.pgdata -w -m immediate stop
'';
}