From 2c0eef8a3a6badcc5ffcd2ddb9e07ac184c5fdb5 Mon Sep 17 00:00:00 2001 From: KIMURA Kazunori Date: Thu, 17 Jul 2025 02:12:29 +0900 Subject: [PATCH 1/2] Replace `awk -F` based csv iteration by IFS + built-in for loop `awk -F,` splits values by comma but built-in for loop reinterpret each values. This behavior does not allow us to contain internal field separator (such as whitespace) in DB_EXTENSION for example. By setting environment variable `IFS` we can control internal field separator that will affect to for-in loop. --- README.md | 2 ++ runtime/functions | 10 ++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b5353bc..a6b88c8 100644 --- a/README.md +++ b/README.md @@ -194,6 +194,8 @@ docker run --name postgresql -itd \ The above command enables the `unaccent` and `pg_trgm` modules on the databases listed in `DB_NAME`, namely `db1` and `db2`. +Each comma separated field can contain whitespace, allowing you to set arbitrary options (such as `CASCADE` ). For full syntax, refer official documentation about [CREATE EXTENSION](https://www.postgresql.org/docs/current/sql-createextension.html). + > **NOTE**: > > This option deprecates the `DB_UNACCENT` parameter. diff --git a/runtime/functions b/runtime/functions index f6e7042..190c48b 100755 --- a/runtime/functions +++ b/runtime/functions @@ -318,10 +318,13 @@ load_extensions() { psql -U ${PG_USER} -d ${database} -c "CREATE EXTENSION IF NOT EXISTS unaccent;" >/dev/null 2>&1 fi - for extension in $(awk -F',' '{for (i = 1 ; i <= NF ; i++) print $i}' <<< "${DB_EXTENSION}"); do + IFS_ORG=$IFS + IFS=, + for extension in ${DB_EXTENSION}; do echo "‣ Loading ${extension} extension..." psql -U ${PG_USER} -d ${database} -c "CREATE EXTENSION IF NOT EXISTS ${extension};" >/dev/null 2>&1 done + IFS=$IFS_ORG } create_database() { @@ -331,7 +334,9 @@ create_database() { echo "INFO! Database cannot be created on a $REPLICATION_MODE node. Skipping..." ;; *) - for database in $(awk -F',' '{for (i = 1 ; i <= NF ; i++) print $i}' <<< "${DB_NAME}"); do + IFS_ORG=$IFS + IFS=, + for database in ${DB_NAME}; do echo "Creating database: ${database}..." if [[ -z $(psql -U ${PG_USER} -Atc "SELECT 1 FROM pg_catalog.pg_database WHERE datname = '${database}'";) ]]; then psql -U ${PG_USER} -c "CREATE DATABASE \"${database}\" WITH TEMPLATE = \"${DB_TEMPLATE}\";" >/dev/null @@ -344,6 +349,7 @@ create_database() { psql -U ${PG_USER} -c "GRANT ALL PRIVILEGES ON DATABASE \"${database}\" to \"${DB_USER}\";" >/dev/null fi done + IFS=$IFS_ORG ;; esac fi From 420ce737cabc66491bfc6cadab112cf1d3e0c9ea Mon Sep 17 00:00:00 2001 From: KIMURA Kazunori Date: Thu, 31 Jul 2025 17:21:27 +0900 Subject: [PATCH 2/2] Fix IFS parsing See pull request kkimurak/docker-postgresql#11 * Fix IFS parsing (Re4zOon) * remove empty line (Re4zOon) * Make temporary variable `IFS_ORG` function-local (kkimurak) Co-authored-by: Re4zOon <9249934+Re4zOon@users.noreply.github.com> --- runtime/functions | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/runtime/functions b/runtime/functions index 190c48b..818cae1 100755 --- a/runtime/functions +++ b/runtime/functions @@ -318,7 +318,7 @@ load_extensions() { psql -U ${PG_USER} -d ${database} -c "CREATE EXTENSION IF NOT EXISTS unaccent;" >/dev/null 2>&1 fi - IFS_ORG=$IFS + local IFS_ORG=$IFS IFS=, for extension in ${DB_EXTENSION}; do echo "‣ Loading ${extension} extension..." @@ -334,7 +334,7 @@ create_database() { echo "INFO! Database cannot be created on a $REPLICATION_MODE node. Skipping..." ;; *) - IFS_ORG=$IFS + local IFS_ORG=$IFS IFS=, for database in ${DB_NAME}; do echo "Creating database: ${database}..."