@@ -31,7 +31,7 @@ _is_sourced() {
3131}
3232
3333# used to create initial posgres directories and if run as root, ensure ownership to the "postgres" user
34- create_postgres_dirs () {
34+ docker_create_database_dirs () {
3535 local user=" $( id -u) "
3636
3737 mkdir -p " $PGDATA "
@@ -56,7 +56,7 @@ create_postgres_dirs() {
5656}
5757
5858# initialize empty PGDATA directory with new database via 'initdb'
59- init_pgdata () {
59+ docker_init_database_dir () {
6060 # "initdb" is particular about the current user existing in "/etc/passwd", so we use "nss_wrapper" to fake that if necessary
6161 # see https://github.com/docker-library/postgres/pull/253, https://github.com/docker-library/postgres/issues/359, https://cwrap.org/nss_wrapper.html
6262 if ! getent passwd " $( id -u) " & > /dev/null && [ -e /usr/lib/libnss_wrapper.so ]; then
@@ -82,7 +82,7 @@ init_pgdata() {
8282}
8383
8484# print large warning if POSTGRES_PASSWORD is empty
85- print_password_warning () {
85+ docker_print_password_warning () {
8686 # check password first so we can output the warning before postgres
8787 # messes it up
8888 if [ " ${# POSTGRES_PASSWORD} " -ge 100 ]; then
@@ -117,9 +117,9 @@ print_password_warning() {
117117}
118118
119119# run, source, or read files from /docker-entrypoint-initdb.d (or specified directory)
120- process_init_files () {
120+ docker_process_init_files () {
121121 # psql here for backwards compatiblilty "${psql[@]}"
122- psql=( psql_run )
122+ psql=( docker_psql_run )
123123
124124 local initDir=" ${1:-/ docker-entrypoint-initdb.d} "
125125
@@ -137,16 +137,16 @@ process_init_files() {
137137 . " $f "
138138 fi
139139 ;;
140- * .sql) echo " $0 : running $f " ; psql_run -f " $f " ; echo ;;
141- * .sql.gz) echo " $0 : running $f " ; gunzip -c " $f " | psql_run ; echo ;;
140+ * .sql) echo " $0 : running $f " ; docker_psql_run -f " $f " ; echo ;;
141+ * .sql.gz) echo " $0 : running $f " ; gunzip -c " $f " | docker_psql_run ; echo ;;
142142 * ) echo " $0 : ignoring $f " ;;
143143 esac
144144 echo
145145 done
146146}
147147
148148# run `psql` with proper arguments for user and db
149- psql_run () {
149+ docker_psql_run () {
150150 local query_runner=( psql -v ON_ERROR_STOP=1 --username " $POSTGRES_USER " --no-password )
151151 if [ -n " $POSTGRES_DB " ]; then
152152 query_runner+=( --dbname " $POSTGRES_DB " )
@@ -157,25 +157,25 @@ psql_run() {
157157
158158# create initial postgresql superuser with password and database
159159# uses environment variables for input: POSTGRES_USER, POSTGRES_PASSWORD, POSTGRES_DB
160- setup_database () {
160+ docker_setup_database () {
161161 if [ " $POSTGRES_DB " != ' postgres' ]; then
162- POSTGRES_DB= psql_run --dbname postgres --set db=" $POSTGRES_DB " << -'EOSQL '
162+ POSTGRES_DB= docker_psql_run --dbname postgres --set db=" $POSTGRES_DB " << -'EOSQL '
163163 CREATE DATABASE :"db" ;
164164 EOSQL
165165 echo
166166 fi
167167}
168168
169169# get user/pass and db from env vars or via file
170- setup_env_vars () {
170+ docker_setup_env_vars () {
171171 file_env ' POSTGRES_PASSWORD'
172172
173173 file_env ' POSTGRES_USER' ' postgres'
174174 file_env ' POSTGRES_DB' " $POSTGRES_USER "
175175}
176176
177177# append md5 or trust auth to pg_hba.conf based on existence of POSTGRES_PASSWORD
178- setup_pg_hba () {
178+ docker_setup_pg_hba () {
179179 local authMethod
180180 if [ " $POSTGRES_PASSWORD " ]; then
181181 authMethod=' md5'
@@ -190,56 +190,56 @@ setup_pg_hba() {
190190}
191191
192192# start socket-only postgresql server for setting up user or running scripts
193- temporary_pgserver_start () {
193+ # all arguments will be passed along as arguments to `postgres` (via pg_ctl)
194+ docker_temporary_pgserver_start () {
194195 # internal start of server in order to allow set-up using psql-client
195- # does not listen on external TCP/IP and waits until start finishes
196+ # does not listen on external TCP/IP and waits until start finishes (can be overridden via args)
196197 PGUSER=" ${PGUSER:- $POSTGRES_USER } " \
197198 pg_ctl -D " $PGDATA " \
198- -o " -c listen_addresses=''" \
199+ -o " -c listen_addresses='' $( [ " $# " -gt 0 ] && printf ' %q ' " $@ " ) " \
199200 -w start
200- # ??? "$@"
201201}
202202
203203# stop postgresql server after done setting up user and running scripts
204- temporary_pgserver_stop () {
204+ docker_temporary_pgserver_stop () {
205205 PGUSER=" ${PGUSER:- postgres} " \
206206 pg_ctl -D " $PGDATA " -m fast -w stop
207207}
208208
209- main () {
209+ _main () {
210210 # if first arg looks like a flag, assume we want to run postgres server
211211 if [ " ${1: 0: 1} " = ' -' ]; then
212212 set -- postgres " $@ "
213213 fi
214214
215215 # setup data directories and permissions, then restart script as postgres user
216216 if [ " $1 " = ' postgres' ] && [ " $( id -u) " = ' 0' ]; then
217- create_postgres_dirs
217+ docker_create_database_dirs
218218 exec gosu postgres " $BASH_SOURCE " " $@ "
219219 fi
220220
221221 if [ " $1 " = ' postgres' ]; then
222- create_postgres_dirs
222+ docker_create_database_dirs
223223
224224 # only run initialization on an empty data directory
225225 # look specifically for PG_VERSION, as it is expected in the DB dir
226226 if [ ! -s " $PGDATA /PG_VERSION" ]; then
227- init_pgdata
227+ docker_init_database_dir
228228
229- setup_env_vars
230- print_password_warning
231- setup_pg_hba
229+ docker_setup_env_vars
230+ docker_print_password_warning
231+ docker_setup_pg_hba
232232
233233 # PGPASSWORD is required for psql when authentication is required for 'local' connections via pg_hba.conf and is otherwise harmless
234234 # e.g. when '--auth=md5' or '--auth-local=md5' is used in POSTGRES_INITDB_ARGS
235235 export PGPASSWORD=" ${PGPASSWORD:- $POSTGRES_PASSWORD } "
236- temporary_pgserver_start
236+ docker_temporary_pgserver_start " ${ @: 2} "
237237
238- setup_database
238+ docker_setup_database
239239
240- process_init_files
240+ docker_process_init_files
241241
242- temporary_pgserver_stop
242+ docker_temporary_pgserver_stop
243243 unset PGPASSWORD
244244
245245 echo
@@ -256,5 +256,5 @@ main() {
256256}
257257
258258if ! _is_sourced; then
259- main " $@ "
259+ _main " $@ "
260260fi
0 commit comments