🔧(postgres) create a user without superuser role

In development environment we wanto tu use the application with a
postgres user not having superuser role. The migration 0027 needs
superuser role to be executed, so we want to be able to test migrations
and other scenarios with a normal user. In order to create this user,
the compose service must be deleted first and then recreated with
DB_USER and DB_PASSWORD environment variable values different with
POSTGRES_USER and POSTGRES_PASSWORD
This commit is contained in:
Manuel Raynaud
2026-05-13 08:18:00 +02:00
parent 562ed0da3e
commit b3ac8b1e8c
3 changed files with 32 additions and 0 deletions
+2
View File
@@ -13,6 +13,8 @@ services:
- env.d/development/postgresql.local
ports:
- "15432:5432"
volumes:
- ./docker/files/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d:ro
redis:
image: redis:5
@@ -0,0 +1,25 @@
#!/bin/bash
set -e
# Create a non-superuser for the application to test migrations.
# Uses DB_USER and DB_PASSWORD from the environment so it stays in
# sync with the application database configuration.
# Only creates the user when DB_USER differs from POSTGRES_USER.
# Validate required environment variables
if [ -z "${POSTGRES_USER}" ] || [ -z "${POSTGRES_DB}" ] || [ -z "${DB_USER}" ] || [ -z "${DB_PASSWORD}" ]; then
echo "Required environment variables (POSTGRES_USER, POSTGRES_DB, DB_USER, DB_PASSWORD) must be set."
exit 0
fi
if [ "${DB_USER}" = "${POSTGRES_USER}" ]; then
echo "DB_USER is the same as POSTGRES_USER, skipping non-superuser creation."
exit 0
fi
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
CREATE USER "${DB_USER}" WITH PASSWORD '${DB_PASSWORD}';
ALTER DATABASE "${POSTGRES_DB}" OWNER TO "${DB_USER}";
EOSQL
+5
View File
@@ -9,3 +9,8 @@ DB_NAME=impress
DB_USER=dinum
DB_PASSWORD=pass
DB_PORT=5432
# If you want to create a user without superuser permissions, you can change it.
# The database must be deleted in order to be recreated.
# DB_USER=docs
# DB_PASSWORD=docs