Search code examples
djangopostgresqldockersuperuser

How to Give A Postgres User SuperUser Previllege Through docker-compose?


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'

Solution

  • 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.