@@ -56,6 +56,7 @@ docker_create_db_directories() {
5656}
5757
5858# initialize empty PGDATA directory with new database via 'initdb'
59+ # arguments to `initdb` can be passed via POSTGRES_INITDB_ARGS or as arguments to this function
5960docker_init_database_dir () {
6061 # "initdb" is particular about the current user existing in "/etc/passwd", so we use "nss_wrapper" to fake that if necessary
6162 # see https://github.com/docker-library/postgres/pull/253, https://github.com/docker-library/postgres/issues/359, https://cwrap.org/nss_wrapper.html
@@ -69,10 +70,10 @@ docker_init_database_dir() {
6970
7071 file_env ' POSTGRES_INITDB_ARGS'
7172 if [ " $POSTGRES_INITDB_WALDIR " ]; then
72- export POSTGRES_INITDB_ARGS= " $POSTGRES_INITDB_ARGS --waldir $POSTGRES_INITDB_WALDIR "
73+ set -- --waldir " $POSTGRES_INITDB_WALDIR " " $@ "
7374 fi
7475
75- eval ' initdb --username="$POSTGRES_USER" --pwfile=<(echo "$POSTGRES_PASSWORD") ' " $POSTGRES_INITDB_ARGS "
76+ eval ' initdb --username="$POSTGRES_USER" --pwfile=<(echo "$POSTGRES_PASSWORD") ' " $POSTGRES_INITDB_ARGS " ' "$@" '
7677
7778 # unset/cleanup "nss_wrapper" bits
7879 if [ " ${LD_PRELOAD:- } " = ' /usr/lib/libnss_wrapper.so' ]; then
@@ -116,15 +117,16 @@ docker_verify_minimum_env() {
116117 fi
117118}
118119
119- # run, source, or read files from /docker-entrypoint-initdb.d (or specified directory)
120+ # usage: docker_process_init_files [file [file [...]]]
121+ # ie: docker_process_init_files /always-initdb.d/*
122+ # process initializer files, based on file extensions and permissions
120123docker_process_init_files () {
121124 # psql here for backwards compatiblilty "${psql[@]}"
122125 psql=( docker_process_sql )
123126
124- local initDir=" ${1:-/ docker-entrypoint-initdb.d} "
125-
126127 echo
127- for f in " ${initDir%/ } " /* ; do
128+ local f
129+ for f; do
128130 case " $f " in
129131 * .sh)
130132 # https://github.com/docker-library/postgres/issues/450#issuecomment-393167936
@@ -192,6 +194,9 @@ pg_setup_hba_conf() {
192194# start socket-only postgresql server for setting up user or running scripts
193195# all arguments will be passed along as arguments to `postgres` (via pg_ctl)
194196docker_temp_server_start () {
197+ if [ " $1 " = ' postgres' ]; then
198+ shift
199+ fi
195200 # internal start of server in order to allow set-up using psql-client
196201 # does not listen on external TCP/IP and waits until start finishes (can be overridden via args)
197202 PGUSER=" ${PGUSER:- $POSTGRES_USER } " \
@@ -214,6 +219,7 @@ _main() {
214219
215220
216221 if [ " $1 " = ' postgres' ]; then
222+ docker_setup_env
217223 # setup data directories and permissions (when run as root)
218224 docker_create_db_directories
219225 if [ " $( id -u) " = ' 0' ]; then
@@ -224,20 +230,18 @@ _main() {
224230 # only run initialization on an empty data directory
225231 # look specifically for PG_VERSION, as it is expected in the DB dir
226232 if [ ! -s " $PGDATA /PG_VERSION" ]; then
227- docker_init_database_dir
228-
229- docker_setup_env
230233 docker_verify_minimum_env
234+ docker_init_database_dir
231235 pg_setup_hba_conf
232236
233237 # PGPASSWORD is required for psql when authentication is required for 'local' connections via pg_hba.conf and is otherwise harmless
234238 # e.g. when '--auth=md5' or '--auth-local=md5' is used in POSTGRES_INITDB_ARGS
235239 export PGPASSWORD=" ${PGPASSWORD:- $POSTGRES_PASSWORD } "
236- docker_temp_server_start " ${ @: 2} "
240+ docker_temp_server_start " $@ "
237241
238242 docker_setup_db
239243
240- docker_process_init_files
244+ docker_process_init_files /docker-entrypoint-initdb.d/ *
241245
242246 docker_temp_server_stop
243247 unset PGPASSWORD
0 commit comments