docker-postgresql icon indicating copy to clipboard operation
docker-postgresql copied to clipboard

Ability to add additional parameters to 'initdb' via POSTGRES_INITDB_ARGS

Open b-a-t opened this issue 8 years ago • 0 comments

Official PG image has an environment variable POSTGRES_INITDB_ARGS that allows to add extra arguments to the call to initdb.

One particular scenario where it is essential is when you need collate and locale settings different from the default C.UTF-8. For example, I need collate to be set to ru_RU.UTF-8. That requires adding such a locale to the image, which can be done by inheriting from your image and additional

RUN localedef --replace -f UTF-8 -i ru_RU ru_RU.UTF-8

Still, you need to change template1(and possibly, template0) to use this locale by default. To achieve that you'll need to add:

--encoding=UTF-8 --lc-collate=ru_RU.utf8 --lc-ctype=ru_RU.utf8

to the initdb call. So, something like:

diff --git a/runtime/env-defaults b/runtime/env-defaults
index 4cff412..374582a 100644
--- a/runtime/env-defaults
+++ b/runtime/env-defaults
@@ -5,6 +5,8 @@ PG_SSL=${PG_SSL:-}
 PG_TRUST_LOCALNET=${PG_TRUST_LOCALNET:-$PSQL_TRUST_LOCALNET} # backward compatibility
 PG_TRUST_LOCALNET=${PG_TRUST_LOCALNET:-false}

+POSTGRES_INITDB_ARGS=${POSTGRES_INITDB_ARGS:-}   # backward compatibility
+
 REPLICATION_MODE=${REPLICATION_MODE:-$PSQL_MODE} # backward compatibility
 REPLICATION_MODE=${REPLICATION_MODE:-}
 REPLICATION_USER=${REPLICATION_USER:-}
diff --git a/runtime/functions b/runtime/functions
index 440c206..bb2d20f 100755
--- a/runtime/functions
+++ b/runtime/functions
@@ -212,7 +212,7 @@ initialize_database() {
         fi

         exec_as_postgres ${PG_BINDIR}/initdb --pgdata=${PG_DATADIR} \
-          --username=${PG_USER} --encoding=unicode --auth=trust ${PG_PASSWORD:+--pwfile=/tmp/pwfile} >/dev/null
+          --username=${PG_USER} --encoding=unicode --auth=trust ${PG_PASSWORD:+--pwfile=/tmp/pwfile} ${POSTGRES_INITDB_ARGS} >/dev/null

         if [[ -n ${PG_OLD_VERSION} ]]; then
           PG_OLD_BINDIR=/usr/lib/postgresql/${PG_OLD_VERSION}/bin

would be great. Of course, you can give other name to the variable, but I guess it's nice to be compatible with the official image as much as possible.

b-a-t avatar Feb 03 '17 13:02 b-a-t