docker-postgresql
docker-postgresql copied to clipboard
Add ability to run .sql and .sh scripts at the container startup after initdb
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
+1