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'

1 answer

  • answered 2019-05-22 05:42 Mihai

    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.