File size: 1,033 Bytes
af3479c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env bash
set -euo pipefail

export PGDATA=/data/postgres

if [[ ! -s "$PGDATA/PG_VERSION" ]]; then
  echo "Initializing Postgres data dir at $PGDATA"
  install -d -m 0700 -o postgres -g postgres "$PGDATA"
  su - postgres -c "/usr/lib/postgresql/*/bin/initdb -D '$PGDATA'"

  # Start temporarily on localhost to run setup
  su - postgres -c "/usr/lib/postgresql/*/bin/pg_ctl -D '$PGDATA' -o \"-c listen_addresses='127.0.0.1'\" -w start"

  DB="regula_db"
  USERNAME="regula"
  PASS="${POSTGRES_PASSWORD:-Regulapasswd#1}"

  su - postgres -c "psql -v ON_ERROR_STOP=1 -c \"CREATE DATABASE $DB;\""
  su - postgres -c "psql -v ON_ERROR_STOP=1 -c \"CREATE USER $USERNAME WITH PASSWORD '$PASS';\""
  su - postgres -c "psql -v ON_ERROR_STOP=1 -c \"GRANT ALL PRIVILEGES ON DATABASE $DB TO $USERNAME;\""

  su - postgres -c "/usr/lib/postgresql/*/bin/pg_ctl -D '$PGDATA' -m fast stop"
fi

# Now run in foreground for supervisor
exec su - postgres -c "/usr/lib/postgresql/*/bin/postgres -D '$PGDATA' -c listen_addresses='127.0.0.1'"