|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +export ENABLE_REPLICATION=${ENABLE_REPLICATION:-false} |
| 4 | + |
| 5 | +set -eu |
| 6 | +export_vars=$(cgroup-limits) ; export $export_vars |
| 7 | + |
| 8 | +source "${CONTAINER_SCRIPTS_PATH}/common.sh" |
| 9 | + |
| 10 | +set_pgdata |
| 11 | + |
| 12 | +process_extending_files \ |
| 13 | + "${APP_DATA}/src/postgresql-pre-start" \ |
| 14 | + "${CONTAINER_SCRIPTS_PATH}/pre-start" |
| 15 | + |
| 16 | +check_env_vars |
| 17 | +generate_passwd_file |
| 18 | +generate_postgresql_config |
| 19 | + |
| 20 | +# Is this brand new data volume? |
| 21 | +PG_INITIALIZED=false |
| 22 | + |
| 23 | +if [ ! -f "$PGDATA/postgresql.conf" ]; then |
| 24 | + initialize_database |
| 25 | + PG_INITIALIZED=: |
| 26 | +else |
| 27 | + try_pgupgrade |
| 28 | +fi |
| 29 | + |
| 30 | +# Use insanely large timeout (24h) to ensure that the potential recovery has |
| 31 | +# enough time here to happen (unless liveness probe kills us). Note that in |
| 32 | +# case of server failure this command still exists immediately. |
| 33 | +pg_ctl start -w --timeout 86400 -o "-h ''" |
| 34 | + |
| 35 | +# This is just a pedantic safety measure (the timeout above is unlikely to |
| 36 | +# happen), but `pt_ctl -w` is not reliable prior to PostgreSQL v10 where it |
| 37 | +# returns exit_status=0 even if the server is still starting. For more info |
| 38 | +# see the issue#297 and |
| 39 | +# https://www.postgresql.org/message-id/CAB7nPqSJs85wK9aknm%3D_jmS6GnH3SQBhpzKcqs8Qo2LhEg2etw%40mail.gmail.com |
| 40 | +pg_isready |
| 41 | + |
| 42 | +if $PG_INITIALIZED ; then |
| 43 | + process_extending_files \ |
| 44 | + "${APP_DATA}/src/postgresql-init" \ |
| 45 | + "${CONTAINER_SCRIPTS_PATH}/init" |
| 46 | + migrate_db |
| 47 | + create_users |
| 48 | +fi |
| 49 | + |
| 50 | +process_extending_files \ |
| 51 | + "${APP_DATA}/src/postgresql-start" \ |
| 52 | + "${CONTAINER_SCRIPTS_PATH}/start" |
| 53 | + |
| 54 | +pg_ctl stop |
| 55 | + |
| 56 | +unset_env_vars |
| 57 | +echo "Starting server..." |
| 58 | +exec postgres "$@" |
0 commit comments