Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 11 additions & 2 deletions runtime/functions
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +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
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
;;
Expand Down
Loading