-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostgres_local.sh
More file actions
executable file
·98 lines (94 loc) · 3.37 KB
/
postgres_local.sh
File metadata and controls
executable file
·98 lines (94 loc) · 3.37 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env bash
# Local Postgres control for the habeas-protocol corpus.
#
# Stores everything under ~/.local/var/pgdata-habeas. Listens on 5433 (so it
# never collides with a system Postgres on 5432). User is your shell $USER.
# No sudo, no Homebrew, no system services.
#
# Usage:
# ./scripts/postgres_local.sh init # one-time: initdb + create database
# ./scripts/postgres_local.sh start # start the server in the background
# ./scripts/postgres_local.sh stop # stop the server
# ./scripts/postgres_local.sh status # is it running?
# ./scripts/postgres_local.sh psql # open psql shell into the habeas db
# ./scripts/postgres_local.sh schema # apply db/schema.sql
# ./scripts/postgres_local.sh reset # drop + recreate the habeas db
# ./scripts/postgres_local.sh nuke # delete the entire data dir
#
# After init+start+schema, run scripts/migrate_to_postgres.py to load the
# corpus. Connection env vars are exported automatically by `eval $(./scripts/postgres_local.sh env)`.
set -euo pipefail
PGDATA="${PGDATA:-$HOME/.local/var/pgdata-habeas}"
PGPORT="${PGPORT:-5433}"
PGHOST="${PGHOST:-localhost}"
PGUSER="${PGUSER:-$USER}"
PGDATABASE="${PGDATABASE:-habeas}"
LOG="${PGDATA}.log"
export PATH="$HOME/.local/bin:$PATH"
cmd=${1:-}
case "$cmd" in
init)
if [ -d "$PGDATA" ]; then
echo "data dir already exists: $PGDATA"
else
mkdir -p "$(dirname "$PGDATA")"
initdb -D "$PGDATA" -U "$PGUSER" --auth=trust --encoding=UTF8 --locale=C
fi
# write a minimal postgresql.conf overlay
grep -q "^port = $PGPORT" "$PGDATA/postgresql.conf" 2>/dev/null || echo "port = $PGPORT" >> "$PGDATA/postgresql.conf"
grep -q "^unix_socket_directories" "$PGDATA/postgresql.conf" 2>/dev/null || echo "unix_socket_directories = '$HOME/.local/var/pg-sock'" >> "$PGDATA/postgresql.conf"
mkdir -p "$HOME/.local/var/pg-sock"
# start, create db, stop
pg_ctl -D "$PGDATA" -l "$LOG" -o "-p $PGPORT" -w start
createdb -h "$PGHOST" -p "$PGPORT" -U "$PGUSER" "$PGDATABASE" 2>/dev/null || echo "(database $PGDATABASE already exists)"
pg_ctl -D "$PGDATA" -m fast stop
echo
echo "initialised. start with: $0 start"
;;
start)
if pg_ctl -D "$PGDATA" status >/dev/null 2>&1; then
echo "already running"
else
pg_ctl -D "$PGDATA" -l "$LOG" -o "-p $PGPORT" -w start
fi
echo "ready: postgres://$PGUSER@$PGHOST:$PGPORT/$PGDATABASE"
;;
stop)
pg_ctl -D "$PGDATA" -m fast stop
;;
status)
pg_ctl -D "$PGDATA" status
;;
psql)
shift || true
exec psql -h "$PGHOST" -p "$PGPORT" -U "$PGUSER" -d "$PGDATABASE" "$@"
;;
schema)
SCHEMA_FILE="$(dirname "$0")/../db/schema.sql"
psql -h "$PGHOST" -p "$PGPORT" -U "$PGUSER" -d "$PGDATABASE" -v ON_ERROR_STOP=1 -f "$SCHEMA_FILE"
;;
reset)
pg_ctl -D "$PGDATA" status >/dev/null 2>&1 && true
dropdb -h "$PGHOST" -p "$PGPORT" -U "$PGUSER" "$PGDATABASE" || true
createdb -h "$PGHOST" -p "$PGPORT" -U "$PGUSER" "$PGDATABASE"
echo "database $PGDATABASE recreated empty"
;;
nuke)
pg_ctl -D "$PGDATA" -m immediate stop 2>/dev/null || true
rm -rf "$PGDATA" "$LOG"
echo "deleted $PGDATA"
;;
env)
cat <<EOF
export PGHOST="$PGHOST"
export PGPORT="$PGPORT"
export PGUSER="$PGUSER"
export PGDATABASE="$PGDATABASE"
export PATH="\$HOME/.local/bin:\$PATH"
EOF
;;
*)
sed -n '2,30p' "$0"
exit 1
;;
esac