4

This is my docker-compose file section for postgres container. These settings are fine, but my django app requires this user to have superuser previlleges through this command inside postgresql.

ALTER ROLE project_admin SUPERUSER;

How can this be accomodated inside this docker-compose file?

 db:
  image: postgres:latest
  container_name: project-db
  environment:
   - POSTGRES_USER='project_admin'
   - POSTGRES_PASS='projectpass'
   - POSTGRES_DB='project'
abbasi_ahsan
  • 340
  • 1
  • 11
Sollosa
  • 379
  • 4
  • 13
  • In Docker Hub, PostgreSQL state clearly for POSTGRES_USER and I quote "This variable will create the specified user with superuser power and a database with the same name". So why do you need to alter the role of 'project_admin' to be a superuser when in the docker compose is already a superuser? – Nader Belal Aug 10 '22 at 17:21

1 Answers1

6

You need to save your command as a script say ./scripts/01_users.sql:

ALTER ROLE project_admin SUPERUSER;

Then your docker-compose:

...
 db:
  image: postgres:latest
  container_name: project-db
  environment:
   - POSTGRES_USER='project_admin'
   - POSTGRES_PASS='projectpass'
   - POSTGRES_DB='project'
  volumes:
   - ./scripts/:/docker-entrypoint-initdb.d/

This will run the script at startup and alter your user's privileges.

Mihai
  • 9,526
  • 2
  • 18
  • 40