diff --git a/postgresql/Dockerfile b/postgresql/Dockerfile index b7667355..1272b5da 100644 --- a/postgresql/Dockerfile +++ b/postgresql/Dockerfile @@ -1,13 +1,15 @@ ARG POSTGRES_VERSION=17 FROM postgres:${POSTGRES_VERSION} +ARG PGRATIONAL_GIT_REPO=https://github.com/begriffs/pg_rational.git ARG PGRATIONAL_VERSION=v0.0.2 WORKDIR /src RUN apt-get update && \ ## Install pg_rational - DEBIAN_FRONTEND=noninteractive apt-get install -y build-essential git postgresql-server-dev-all pgloader && \ - git clone https://github.com/begriffs/pg_rational.git && \ + DEBIAN_FRONTEND=noninteractive apt-get install -y build-essential git \ + postgresql-server-dev-all pgloader && \ + git clone ${PGRATIONAL_GIT_REPO} && \ cd pg_rational && \ git checkout ${PGRATIONAL_VERSION} && \ make && \ diff --git a/postgresql/Makefile b/postgresql/Makefile index 880012f1..1cb3a541 100644 --- a/postgresql/Makefile +++ b/postgresql/Makefile @@ -14,7 +14,7 @@ psql: .PHONY: shell shell: - @make --no-print-directory docker compose-shell SERVICE=psotgres + @make --no-print-directory docker compose-shell SERVICE=postgres .PHONY: client # Get client certificate and private key files client: @@ -66,7 +66,12 @@ localdb: client .PHONY: chinook # Import Chinook sample database chinook: - docker compose --env-file=${ENV_FILE} exec postgres /bin/sh -c 'createdb chinook && psql -c "create role chinook; grant chinook to $${POSTGRES_DB};" && PGUSER=$${POSTGRES_DB} pgloader https://github.com/lerocha/chinook-database/raw/master/ChinookDatabase/DataSources/Chinook_Sqlite_AutoIncrementPKs.sqlite pgsql:///chinook' + @POSTGRES_DB=$$(${BIN}/dotenv -f ${ENV_FILE} get POSTGRES_DB); set -x; docker compose --env-file=${ENV_FILE} exec postgres /bin/sh -c 'createdb chinook && PGUSER=root pgloader https://github.com/lerocha/chinook-database/raw/master/ChinookDatabase/DataSources/Chinook_Sqlite_AutoIncrementPKs.sqlite pgsql:///chinook && psql -c "create role chinook;" && psql -d chinook -c "GRANT chinook to $${POSTGRES_DB}; GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO chinook; GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO chinook;"' + +.PHONY: drop-chinook # Delete the chinook sample database +drop-chinook: + docker compose --env-file=${ENV_FILE} exec postgres /bin/sh -c 'psql -X -c "drop database chinook"' || true + docker compose --env-file=${ENV_FILE} exec postgres /bin/sh -c 'psql -X -c "drop role chinook"' || true .PHONY: psql-alias # Get the psql alias with baked connection string psql-alias: diff --git a/postgresql/init-user.sh b/postgresql/init-user.sh index 6ebc9c59..6d596524 100644 --- a/postgresql/init-user.sh +++ b/postgresql/init-user.sh @@ -4,5 +4,9 @@ set -e ## hook to create the POSTGRES_LIMITED_USER on fresh DB initialization. psql -v ON_ERROR_STOP=1 --username "${POSTGRES_USER}" --dbname "${POSTGRES_DB}" <<-EOF CREATE USER "${POSTGRES_LIMITED_USER}"; + GRANT USAGE ON SCHEMA public TO "${POSTGRES_LIMITED_USER}"; + GRANT CREATE ON SCHEMA public TO "${POSTGRES_LIMITED_USER}"; + ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL PRIVILEGES ON TABLES TO ${POSTGRES_LIMITED_USER}; + ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL PRIVILEGES ON SEQUENCES TO ${POSTGRES_LIMITED_USER}; CREATE EXTENSION pg_rational; EOF