Skip to content

Commit 6e5f997

Browse files
committed
Add parameter DB_USER_IS_DB_OWNER (boolean)
If it is set to true, issue `ALTER DATABASE .. OWNER TO ${DB_USER}`
1 parent 192912b commit 6e5f997

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ docker run --name postgresql -itd --restart always \
182182

183183
In the above example `dbuser` with be granted access to both the `dbname1` and `dbname2` databases.
184184

185+
If you want `DB_USER` to have ownership of database listed in `DB_NAME`, you can set `DB_USER_IS_DB_NAME` to true.
186+
185187
# Enabling extensions
186188

187189
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.

runtime/functions

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -340,11 +340,17 @@ create_database() {
340340
load_extensions ${database}
341341

342342
if [[ -n ${DB_USER} ]]; then
343-
echo "‣ Granting access to ${DB_USER} user..."
344-
psql -U ${PG_USER} -c "GRANT ALL PRIVILEGES ON DATABASE \"${database}\" to \"${DB_USER}\";" >/dev/null
345-
346-
echo "‣ Granting access on public schema to user '${DB_USER}'"
347-
psql -U ${PG_USER} -c "GRANT ALL ON SCHEMA public TO \"${DB_USER}\";" -d "${database}" >/dev/null
343+
if [[ "${DB_USER_IS_DB_OWNER}" == true ]]; then
344+
echo "‣ Setting ${DB_USER} as an owner of database ${database}..."
345+
psql -U ${PG_USER} -c "ALTER DATABASE ${database} OWNER TO ${DB_USER};" >/dev/null
346+
# now DB_USER have access to table / schema where the owner is pg_database_owner (e.g. 'public' schema)
347+
else
348+
echo "‣ Granting access to ${DB_USER} user..."
349+
psql -U ${PG_USER} -c "GRANT ALL PRIVILEGES ON DATABASE \"${database}\" to \"${DB_USER}\";" >/dev/null
350+
351+
echo "‣ Granting access on public schema to user '${DB_USER}'"
352+
psql -U ${PG_USER} -c "GRANT ALL ON SCHEMA public TO \"${DB_USER}\";" -d "${database}" >/dev/null
353+
fi
348354
fi
349355
done
350356
;;

0 commit comments

Comments
 (0)