New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to run .sql and .sh scripts at the container startup after initdb #90

Open
b-a-t opened this Issue Feb 3, 2017 · 1 comment

Comments

Projects
None yet
2 participants
@b-a-t

b-a-t commented Feb 3, 2017

Official image allows to run arbitary *.sql and *.sh scripts after initialization with initdb, but before full production start of the container. Having similar feature with your image would be great too. That would resolve, for example, #54 and #70 without adding extra logic to the image.

Library image uses /docker-entrypoint-initdb.d/ directory as a place where all such code snippets should be placed. That can be done, for example in the inherited image. Something like this can be added(possibly requires more sanity checks?):

diff --git a/runtime/functions b/runtime/functions
index 440c206..448ac1d 100755
--- a/runtime/functions
+++ b/runtime/functions
@@ -284,6 +284,18 @@ configure_recovery() {
   fi
 }

+initdb_scripts() {
+  echo "Running DB initialization scripts..."
+  for file in /docker-entrypoint-initdb.d/*; do
+    case "$file" in
+      *.sh)     echo "running $file"; . "$file";;
+      *.sql)    echo "running $file"; psql -U ${PG_USER} -f "$file" >/dev/null 2>&1; echo;;
+      *.sql.gz) echo "running $file"; gunzip -c "$file" | psql -U ${PG_USER} >/dev/null 2>&1; echo;;
+      *)        echo "ignoring $file";;
+    esac
+  done
+}
+
 create_user() {
   if [[ -n ${DB_USER} ]]; then
     case $REPLICATION_MODE in
@@ -379,6 +391,7 @@ configure_postgresql() {
   set_postgresql_param "listen_addresses" "127.0.0.1" quiet
   exec_as_postgres ${PG_BINDIR}/pg_ctl -D ${PG_DATADIR} -w start >/dev/null

+  initdb_scripts
   create_user
   create_database
   create_replication_user
@ashish235

This comment has been minimized.

ashish235 commented Jun 11, 2018

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment