docs/docker/files/docker-entrypoint-initdb.d/01_create_app_user.sh
Manuel Raynaud b3ac8b1e8c
🔧(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
2026-05-13 08:18:00 +02:00

25 lines
902 B
Bash
Executable file

#!/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