|
@@ -0,0 +1,145 @@
|
|
1
|
+#!/bin/bash
|
|
2
|
+set -e
|
|
3
|
+
|
|
4
|
+# usage: file_env VAR [DEFAULT]
|
|
5
|
+# ie: file_env 'XYZ_DB_PASSWORD' 'example'
|
|
6
|
+# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
|
|
7
|
+# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
|
|
8
|
+file_env() {
|
|
9
|
+ local var="$1"
|
|
10
|
+ local fileVar="${var}_FILE"
|
|
11
|
+ local def="${2:-}"
|
|
12
|
+ if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
|
|
13
|
+ echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
|
|
14
|
+ exit 1
|
|
15
|
+ fi
|
|
16
|
+ local val="$def"
|
|
17
|
+ if [ "${!var:-}" ]; then
|
|
18
|
+ val="${!var}"
|
|
19
|
+ elif [ "${!fileVar:-}" ]; then
|
|
20
|
+ val="$(< "${!fileVar}")"
|
|
21
|
+ fi
|
|
22
|
+ export "$var"="$val"
|
|
23
|
+ unset "$fileVar"
|
|
24
|
+}
|
|
25
|
+
|
|
26
|
+if [ "${1:0:1}" = '-' ]; then
|
|
27
|
+ set -- postgres "$@"
|
|
28
|
+fi
|
|
29
|
+
|
|
30
|
+# allow the container to be started with `--user`
|
|
31
|
+if [ "$1" = 'postgres' ] && [ "$(id -u)" = '0' ]; then
|
|
32
|
+ mkdir -p "$PGDATA"
|
|
33
|
+ chown -R postgres "$PGDATA"
|
|
34
|
+ chmod 700 "$PGDATA"
|
|
35
|
+
|
|
36
|
+ mkdir -p /var/run/postgresql
|
|
37
|
+ chown -R postgres /var/run/postgresql
|
|
38
|
+ chmod 775 /var/run/postgresql
|
|
39
|
+
|
|
40
|
+ # Create the transaction log directory before initdb is run (below) so the directory is owned by the correct user
|
|
41
|
+ if [ "$POSTGRES_INITDB_XLOGDIR" ]; then
|
|
42
|
+ mkdir -p "$POSTGRES_INITDB_XLOGDIR"
|
|
43
|
+ chown -R postgres "$POSTGRES_INITDB_XLOGDIR"
|
|
44
|
+ chmod 700 "$POSTGRES_INITDB_XLOGDIR"
|
|
45
|
+ fi
|
|
46
|
+
|
|
47
|
+ exec gosu postgres "$BASH_SOURCE" "$@"
|
|
48
|
+fi
|
|
49
|
+
|
|
50
|
+if [ "$1" = 'postgres' ]; then
|
|
51
|
+ mkdir -p "$PGDATA"
|
|
52
|
+ chown -R "$(id -u)" "$PGDATA" 2>/dev/null || :
|
|
53
|
+ chmod 700 "$PGDATA" 2>/dev/null || :
|
|
54
|
+
|
|
55
|
+ # look specifically for PG_VERSION, as it is expected in the DB dir
|
|
56
|
+ if [ ! -s "$PGDATA/PG_VERSION" ]; then
|
|
57
|
+ file_env 'POSTGRES_INITDB_ARGS'
|
|
58
|
+ if [ "$POSTGRES_INITDB_XLOGDIR" ]; then
|
|
59
|
+ export POSTGRES_INITDB_ARGS="$POSTGRES_INITDB_ARGS --xlogdir $POSTGRES_INITDB_XLOGDIR"
|
|
60
|
+ fi
|
|
61
|
+ eval "initdb --username=postgres $POSTGRES_INITDB_ARGS"
|
|
62
|
+
|
|
63
|
+ # check password first so we can output the warning before postgres
|
|
64
|
+ # messes it up
|
|
65
|
+ file_env 'POSTGRES_PASSWORD'
|
|
66
|
+ if [ "$POSTGRES_PASSWORD" ]; then
|
|
67
|
+ pass="PASSWORD '$POSTGRES_PASSWORD'"
|
|
68
|
+ authMethod=md5
|
|
69
|
+ else
|
|
70
|
+ # The - option suppresses leading tabs but *not* spaces. :)
|
|
71
|
+ cat >&2 <<-'EOWARN'
|
|
72
|
+ ****************************************************
|
|
73
|
+ WARNING: No password has been set for the database.
|
|
74
|
+ This will allow anyone with access to the
|
|
75
|
+ Postgres port to access your database. In
|
|
76
|
+ Docker's default configuration, this is
|
|
77
|
+ effectively any other container on the same
|
|
78
|
+ system.
|
|
79
|
+
|
|
80
|
+ Use "-e POSTGRES_PASSWORD=password" to set
|
|
81
|
+ it in "docker run".
|
|
82
|
+ ****************************************************
|
|
83
|
+ EOWARN
|
|
84
|
+
|
|
85
|
+ pass=
|
|
86
|
+ authMethod=trust
|
|
87
|
+ fi
|
|
88
|
+
|
|
89
|
+ {
|
|
90
|
+ echo
|
|
91
|
+ echo "host all all all $authMethod"
|
|
92
|
+ } >> "$PGDATA/pg_hba.conf"
|
|
93
|
+
|
|
94
|
+ # internal start of server in order to allow set-up using psql-client
|
|
95
|
+ # does not listen on external TCP/IP and waits until start finishes
|
|
96
|
+ PGUSER="${PGUSER:-postgres}" \
|
|
97
|
+ pg_ctl -D "$PGDATA" \
|
|
98
|
+ -o "-c listen_addresses='localhost'" \
|
|
99
|
+ -w start
|
|
100
|
+
|
|
101
|
+ file_env 'POSTGRES_USER' 'postgres'
|
|
102
|
+ file_env 'POSTGRES_DB' "$POSTGRES_USER"
|
|
103
|
+
|
|
104
|
+ psql=( psql -v ON_ERROR_STOP=1 )
|
|
105
|
+
|
|
106
|
+ if [ "$POSTGRES_DB" != 'postgres' ]; then
|
|
107
|
+ "${psql[@]}" --username postgres <<-EOSQL
|
|
108
|
+ CREATE DATABASE "$POSTGRES_DB" ;
|
|
109
|
+ EOSQL
|
|
110
|
+ echo
|
|
111
|
+ fi
|
|
112
|
+
|
|
113
|
+ if [ "$POSTGRES_USER" = 'postgres' ]; then
|
|
114
|
+ op='ALTER'
|
|
115
|
+ else
|
|
116
|
+ op='CREATE'
|
|
117
|
+ fi
|
|
118
|
+ "${psql[@]}" --username postgres <<-EOSQL
|
|
119
|
+ $op USER "$POSTGRES_USER" WITH SUPERUSER $pass ;
|
|
120
|
+ EOSQL
|
|
121
|
+ echo
|
|
122
|
+
|
|
123
|
+ psql+=( --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" )
|
|
124
|
+
|
|
125
|
+ echo
|
|
126
|
+ for f in /docker-entrypoint-initdb.d/*; do
|
|
127
|
+ case "$f" in
|
|
128
|
+ *.sh) echo "$0: running $f"; . "$f" ;;
|
|
129
|
+ *.sql) echo "$0: running $f"; "${psql[@]}" -f "$f"; echo ;;
|
|
130
|
+ *.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${psql[@]}"; echo ;;
|
|
131
|
+ *) echo "$0: ignoring $f" ;;
|
|
132
|
+ esac
|
|
133
|
+ echo
|
|
134
|
+ done
|
|
135
|
+
|
|
136
|
+ PGUSER="${PGUSER:-postgres}" \
|
|
137
|
+ pg_ctl -D "$PGDATA" -m fast -w stop
|
|
138
|
+
|
|
139
|
+ echo
|
|
140
|
+ echo 'PostgreSQL init process complete; ready for start up.'
|
|
141
|
+ echo
|
|
142
|
+ fi
|
|
143
|
+fi
|
|
144
|
+
|
|
145
|
+exec "$@"
|