Skip to content

Commit

Permalink
more sane way of starting and stopping postgres server using pg_ctl a…
Browse files Browse the repository at this point in the history
…s recommended per postgresql docs
  • Loading branch information
renzok committed Aug 4, 2015
1 parent a82c28e commit d49499c
Showing 1 changed file with 8 additions and 22 deletions.
30 changes: 8 additions & 22 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ set_listen_addresses() {
}

if [ "$1" = 'postgres' ]; then
mkdir -p "$PGDATA"
chown -R postgres "$PGDATA"
mkdir -p "${PGDATA}"
chown -R postgres "${PGDATA}"

chmod g+s /run/postgresql
chown -R postgres /run/postgresql
Expand Down Expand Up @@ -44,21 +44,11 @@ if [ "$1" = 'postgres' ]; then

{ echo; echo "host all all 0.0.0.0/0 $authMethod"; } >> "$PGDATA/pg_hba.conf"

set_listen_addresses '' # we're going to start up postgres, but it's not ready for use yet (this is initialization), so don't listen to the outside world yet

gosu postgres "$@" &
pid="$!"
for i in {30..0}; do
if echo 'SELECT 1' | psql --username postgres &> /dev/null; then
break
fi
echo 'PostgreSQL init process in progress...'
sleep 1
done
if [ "$i" = 0 ]; then
echo >&2 'PostgreSQL init process failed'
exit 1
fi
# internal start of server in order to allow set-up using psql-client
gosu postgres pg_ctl -D ${PGDATA} \
-o "-c listen_addresses=''" \
-w start # does not listen on TCP/IP and wait
# until start finished

: ${POSTGRES_USER:=postgres}
: ${POSTGRES_DB:=$POSTGRES_USER}
Expand Down Expand Up @@ -91,11 +81,7 @@ if [ "$1" = 'postgres' ]; then
echo
done

if ! kill -s TERM "$pid" || ! wait "$pid"; then
echo >&2 'PostgreSQL init process failed'
exit 1
fi

gosu postgres pg_ctl -D ${PGDATA} -m fast -w stop
set_listen_addresses '*'

echo
Expand Down

0 comments on commit d49499c

Please sign in to comment.