From 192912b9d8356ad694904963221c14774c0ae054 Mon Sep 17 00:00:00 2001 From: Kazunori Kimura Date: Sun, 18 May 2025 00:32:34 +0900 Subject: [PATCH 1/2] GRANT ALL ON SCHEMA public TO ${DB_USER} fix: specify database to GRANT ALL ON SCHEMA public TO ${DB_USER} --- runtime/functions | 3 +++ 1 file changed, 3 insertions(+) diff --git a/runtime/functions b/runtime/functions index f6e7042..433de51 100755 --- a/runtime/functions +++ b/runtime/functions @@ -342,6 +342,9 @@ create_database() { if [[ -n ${DB_USER} ]]; then echo "‣ Granting access to ${DB_USER} user..." psql -U ${PG_USER} -c "GRANT ALL PRIVILEGES ON DATABASE \"${database}\" to \"${DB_USER}\";" >/dev/null + + echo "‣ Granting access on public schema to user '${DB_USER}'" + psql -U ${PG_USER} -c "GRANT ALL ON SCHEMA public TO \"${DB_USER}\";" -d "${database}" >/dev/null fi done ;; From 903331e8e4a91313f827d3f8257abd7d8d23a337 Mon Sep 17 00:00:00 2001 From: KIMURA Kazunori Date: Thu, 17 Jul 2025 02:11:37 +0900 Subject: [PATCH 2/2] Add parameter `DB_USER_IS_DB_OWNER` (boolean) If it is set to true, issue `ALTER DATABASE .. OWNER TO ${DB_USER}` --- README.md | 2 ++ runtime/functions | 16 +++++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index b5353bc..fb02113 100644 --- a/README.md +++ b/README.md @@ -182,6 +182,8 @@ docker run --name postgresql -itd --restart always \ In the above example `dbuser` with be granted access to both the `dbname1` and `dbname2` databases. +If you want `DB_USER` to have ownership of database listed in `DB_NAME`, you can set `DB_USER_IS_DB_OWNER` to true. + # Enabling extensions The image also packages the [postgres contrib module](http://www.postgresql.org/docs/9.4/static/contrib.html). A comma separated list of modules can be specified using the `DB_EXTENSION` parameter. diff --git a/runtime/functions b/runtime/functions index 433de51..86baa13 100755 --- a/runtime/functions +++ b/runtime/functions @@ -340,11 +340,17 @@ create_database() { load_extensions ${database} if [[ -n ${DB_USER} ]]; then - echo "‣ Granting access to ${DB_USER} user..." - psql -U ${PG_USER} -c "GRANT ALL PRIVILEGES ON DATABASE \"${database}\" to \"${DB_USER}\";" >/dev/null - - echo "‣ Granting access on public schema to user '${DB_USER}'" - psql -U ${PG_USER} -c "GRANT ALL ON SCHEMA public TO \"${DB_USER}\";" -d "${database}" >/dev/null + if [[ "${DB_USER_IS_DB_OWNER}" == true ]]; then + echo "‣ Setting ${DB_USER} as an owner of database ${database}..." + psql -U ${PG_USER} -c "ALTER DATABASE ${database} OWNER TO ${DB_USER};" >/dev/null + # now DB_USER have access to table / schema where the owner is pg_database_owner (e.g. 'public' schema) + else + echo "‣ Granting access to ${DB_USER} user..." + psql -U ${PG_USER} -c "GRANT ALL PRIVILEGES ON DATABASE \"${database}\" to \"${DB_USER}\";" >/dev/null + + echo "‣ Granting access on public schema to user '${DB_USER}'" + psql -U ${PG_USER} -c "GRANT ALL ON SCHEMA public TO \"${DB_USER}\";" -d "${database}" >/dev/null + fi fi done ;;