|
| 1 | +#!/bin/bash |
| 2 | +set -e |
| 3 | + |
| 4 | +if [ "${1:0:1}" = '-' ]; then |
| 5 | + set -- postgres "$@" |
| 6 | +fi |
| 7 | + |
| 8 | +if [ "$1" = 'postgres' ]; then |
| 9 | + mkdir -p "$PGDATA" |
| 10 | + chmod 700 "$PGDATA" |
| 11 | + chown -R postgres "$PGDATA" |
| 12 | + |
| 13 | + chmod g+s /run/postgresql |
| 14 | + chown -R postgres /run/postgresql |
| 15 | + |
| 16 | + # look specifically for PG_VERSION, as it is expected in the DB dir |
| 17 | + if [ ! -s "$PGDATA/PG_VERSION" ]; then |
| 18 | + eval "gosu postgres initdb $POSTGRES_INITDB_ARGS" |
| 19 | + |
| 20 | + # check password first so we can output the warning before postgres |
| 21 | + # messes it up |
| 22 | + if [ "$POSTGRES_PASSWORD" ]; then |
| 23 | + pass="PASSWORD '$POSTGRES_PASSWORD'" |
| 24 | + authMethod=md5 |
| 25 | + else |
| 26 | + # The - option suppresses leading tabs but *not* spaces. :) |
| 27 | + cat >&2 <<-'EOWARN' |
| 28 | + **************************************************** |
| 29 | + WARNING: No password has been set for the database. |
| 30 | + This will allow anyone with access to the |
| 31 | + Postgres port to access your database. In |
| 32 | + Docker's default configuration, this is |
| 33 | + effectively any other container on the same |
| 34 | + system. |
| 35 | +
|
| 36 | + Use "-e POSTGRES_PASSWORD=password" to set |
| 37 | + it in "docker run". |
| 38 | + **************************************************** |
| 39 | + EOWARN |
| 40 | + |
| 41 | + pass= |
| 42 | + authMethod=trust |
| 43 | + fi |
| 44 | + |
| 45 | + { echo; echo "host all all 0.0.0.0/0 $authMethod"; } >> "$PGDATA/pg_hba.conf" |
| 46 | + |
| 47 | + # internal start of server in order to allow set-up using psql-client |
| 48 | + # does not listen on external TCP/IP and waits until start finishes |
| 49 | + gosu postgres pg_ctl -D "$PGDATA" \ |
| 50 | + -o "-c listen_addresses='localhost'" \ |
| 51 | + -w start |
| 52 | + |
| 53 | + : ${POSTGRES_USER:=postgres} |
| 54 | + : ${POSTGRES_DB:=$POSTGRES_USER} |
| 55 | + export POSTGRES_USER POSTGRES_DB |
| 56 | + |
| 57 | + psql=( psql -v ON_ERROR_STOP=1 ) |
| 58 | + |
| 59 | + if [ "$POSTGRES_DB" != 'postgres' ]; then |
| 60 | + "${psql[@]}" --username postgres <<-EOSQL |
| 61 | + CREATE DATABASE "$POSTGRES_DB" ; |
| 62 | + EOSQL |
| 63 | + echo |
| 64 | + fi |
| 65 | + |
| 66 | + if [ "$POSTGRES_USER" = 'postgres' ]; then |
| 67 | + op='ALTER' |
| 68 | + else |
| 69 | + op='CREATE' |
| 70 | + fi |
| 71 | + "${psql[@]}" --username postgres <<-EOSQL |
| 72 | + $op USER "$POSTGRES_USER" WITH SUPERUSER $pass ; |
| 73 | + EOSQL |
| 74 | + echo |
| 75 | + |
| 76 | + psql+=( --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" ) |
| 77 | + |
| 78 | + echo |
| 79 | + for f in /docker-entrypoint-initdb.d/*; do |
| 80 | + case "$f" in |
| 81 | + *.sh) echo "$0: running $f"; . "$f" ;; |
| 82 | + *.sql) echo "$0: running $f"; "${psql[@]}" < "$f"; echo ;; |
| 83 | + *.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${psql[@]}"; echo ;; |
| 84 | + *) echo "$0: ignoring $f" ;; |
| 85 | + esac |
| 86 | + echo |
| 87 | + done |
| 88 | + |
| 89 | + gosu postgres pg_ctl -D "$PGDATA" -m fast -w stop |
| 90 | + |
| 91 | + echo |
| 92 | + echo 'PostgreSQL init process complete; ready for start up.' |
| 93 | + echo |
| 94 | + fi |
| 95 | + |
| 96 | + exec gosu postgres "$@" |
| 97 | +fi |
| 98 | + |
| 99 | +exec "$@" |
0 commit comments