forked from PrairieLearn/PrairieLearn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart_postgres.sh
executable file
·40 lines (33 loc) · 939 Bytes
/
start_postgres.sh
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
#!/bin/bash
export PGDATA=${PGDATA:=/var/postgres}
if [[ -z "$1" ]]; then
ACTION=start
else
ACTION=$1
fi
# if we are trying to start but postgres is already running, exit with no action
if [[ "$ACTION" == "start" ]]; then
if pg_isready -q ; then
exit
fi
fi
mkdir -p $PGDATA
chown -f postgres:postgres $PGDATA
su postgres -c 'pg_ctl status' >/dev/null 2>&1
if [[ $? == 4 ]]; then
echo "Making new postgres database at ${PGDATA}"
su postgres -c "initdb" >/dev/null 2>&1
INIT_RESOLVE=0
ACTION=start
fi
# Only locally start postgres if we weren't given a PGHOST environment variable
if [[ -z "$PGHOST" ]]; then
su postgres -c "pg_ctl --silent --log=${PGDATA}/postgresql.log ${ACTION}"
fi
if [[ "$ACTION" == "start" ]]; then
# wait for postgres to start (local or PGHOST)
until pg_isready -q ; do sleep 1 ; done
fi
if [[ $INIT_RESOLVE ]]; then
su postgres -c "createuser -s root"
fi