David Pekker
David Pekker

Reputation: 353

create a user in postgres docker container

I am trying to create a django app in docker container, but I do not know how to create a user in postgres container that django uses. I guess that is why when I start it (by docker-compose up --build --force-recreate) it tells me database_1 | FATAL: role "my_user" does not exist. What am I doing wrong?

docker-compose.yaml right now looks like:

version: "3.1"
services:

  database:
    image: postgres:9.5
    environment:
      POSTGRES_DB: "django_db"
      POSTGRES_USER: "my_user"
      POSTGRES_PASSWORD: "my_password"
      POSTGRES_DATA: /var/lib/postgresql/data/pgdata
    logging:
      options:
        max-size: "10m"
        max-file: "3"
    restart: always
    volumes:
      - "database:/var/lib/postgresql/data"
    tmpfs:
      - "/tmp"
      - "/run"
    networks:
      db-net:

  django:
    build: ./
    image: django:latest
    environment:
      DATABASE_URL: "postgres://my_user:my_pass@database/django_db"

    ports:
      - 7445:80

Upvotes: 1

Views: 3046

Answers (1)

Yannic Hamann
Yannic Hamann

Reputation: 5225

From your docker-compose.yml I would say that you have successfully created the user my_user in your database. You can verify that by running docker exec -ti project_database_1 psql -U my_user -d django_db on your host.

I suggest you take a look at cookiecutter-django. You can find there a complete docker setup including the solved problem JodyT mentioned about starting Django before the postgres container is actually ready.

It's from the authors of the book two scopes of django which is also worth a read.

Upvotes: 3

Related Questions