Skip to content

Commit 9065a59

Browse files
committed
adds script
1 parent 9f2ddb9 commit 9065a59

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

services/postgres/Makefile

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
include ../../scripts/common.Makefile
2+
3+
4+
ifneq (,$(wildcard $(DOT_ENV_FILE)))
5+
include $(DOT_ENV_FILE)
6+
export $(shell sed 's/=.*//' $(DOT_ENV_FILE))
7+
endif
8+
9+
10+
readonly_user=${POSTGRES_READONLY_USER}
11+
readonly_password=${POSTGRES_READONLY_PASSWORD}
12+
database=${POSTGRES_DB}
13+
schema=$(if $(POSTGRES_SCHEMA),$(POSTGRES_SCHEMA),public)
14+
15+
.PHONY: readonly-user-sql
16+
readonly-user-sql: ## ql-script to create a new readonly user
17+
@echo " -- Creating read-only user ${readonly_user} for ${database}.${schema}"
18+
@echo
19+
@echo " --Create the read-only user with a password"
20+
@echo "CREATE USER \"${readonly_user}\" WITH PASSWORD '${readonly_password}';"
21+
@echo " --Grant CONNECT privilege to the database (e.g., 'foo' is the database name)"
22+
@echo "GRANT CONNECT ON DATABASE ${database} TO \"${readonly_user}\";"
23+
@echo " --Grant USAGE privilege on the public schema"
24+
@echo "GRANT USAGE ON SCHEMA ${schema} TO \"${readonly_user}\";"
25+
@echo " --Grant SELECT privilege on all existing tables in the public schema"
26+
@echo "GRANT SELECT ON ALL TABLES IN SCHEMA ${schema} TO \"${readonly_user}\";"
27+
@echo " --Grant SELECT privilege on all existing sequences in the public schema"
28+
@echo "GRANT SELECT ON ALL SEQUENCES IN SCHEMA ${schema} TO \"${readonly_user}\";"
29+
@echo " --Ensure that future tables created in the public schema will have SELECT privilege for the read-only user"
30+
@echo "ALTER DEFAULT PRIVILEGES IN SCHEMA ${schema} GRANT SELECT ON TABLES TO \"${readonly_user}\";"
31+
@echo " --Ensure that future sequences created in the public schema will have SELECT privilege for the read-only user"
32+
@echo "ALTER DEFAULT PRIVILEGES IN SCHEMA ${schema} GRANT SELECT ON SEQUENCES TO \"${readonly_user}\";"
33+
@echo
34+
@echo " -- Listing all users"
35+
@echo "SELECT * FROM pg_roles;"

services/postgres/docker-entrypoint-initdb.d/create-readonly-user.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fi
1717
readonly_user=${POSTGRES_READONLY_USER}
1818
readonly_password=${POSTGRES_READONLY_PASSWORD}
1919
database=${POSTGRES_DB}
20-
schema=${SCHEMA:-public}
20+
schema=${POSTGRES_SCHEMA:-public}
2121

2222
# Create the read-only user and assign permissions
2323
echo "Creating read-only user: $readonly_user for $database.$schema ..."
@@ -29,4 +29,5 @@ psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$database" <<-EOSQ
2929
GRANT SELECT ON ALL TABLES IN SCHEMA $schema TO "$readonly_user";
3030
GRANT SELECT ON ALL SEQUENCES IN SCHEMA $schema TO "$readonly_user";
3131
ALTER DEFAULT PRIVILEGES IN SCHEMA $schema GRANT SELECT ON TABLES TO "$readonly_user";
32+
ALTER DEFAULT PRIVILEGES IN SCHEMA $schema GRANT SELECT ON SEQUENCES TO "$readonly_user";
3233
EOSQL

0 commit comments

Comments
 (0)