6

This is my docker-compose file section for postgres container. These settings are fine, but my django app requires this user to have superuser privileges 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'
1
  • 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? Commented Aug 10, 2022 at 17:21

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Mihai, one more thing. Do I need to turn executable bit on this script or would it run anyway?
It's .sql so it runs anyway

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.