testdrivenio/django-on-docker

DB role doesn't exist

Emnalyeriar opened this issue · 5 comments

Hey,
before this tutorial, I was using the default PostgreSQL superuser simply named postgres and when I changed it to something I get this error:
django.db.utils.OperationalError: FATAL: role "hello_django" does not exist
I guess you need to create this user first but I haven't seen that mentioned in the article anywhere.

Change the environment variables for the db service:

    environment:
      - POSTGRES_USER=hello_django
      - POSTGRES_PASSWORD=hello_django
      - POSTGRES_DB=hello_django_dev

Review the env var section of https://hub.docker.com/_/postgres for more info.

Hey, thanks for the response but I'm afraid this doesn't solve my problem. According to the link you provided a user and DB with those names should be created. But somehow it still produces the same error.

Here is my config:
.env (this part of env that is visible in django settings)

POSTGRES_DATABASE=django
POSTGRES_USER=django
POSTGRES_PASSWORD=django
POSTGRES_HOST=db
POSTGRES_PORT=5432

docker-compose.yml (env vars set here should create this user and db when image is first run)

...
  db:
    image: postgres:12.0-alpine
    volumes:
     - pg_data:/var/lib/postgresql/data
    environment:
      - POSTGRES_USER=django
      - POSTGRES_PASSWORD=django
      - POSTGRES_DB=django
...

django settings

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': env('POSTGRES_DATABASE', default='django'),
        'USER': env('POSTGRES_USER', default='django'),
        'PASSWORD': env('POSTGRES_PASSWORD', default='django'),
        'HOST': env('POSTGRES_HOST', default='db'),
        'PORT': env('POSTGRES_PORT', default='5432'),
    }
}

Unfortunately I still have this error:
django.db.utils.OperationalError: FATAL: role "django" does not exist

The volume probably needs to come down. Bring the volume down and try again. (docker-compose down -v)

I was pretty sure I did this but before posting here I tried again with:
docker rm $(docker ps -a -q) -f
docker volume prune
and look like it worked, thanks!

Hey I'm still getting the same issue trying each step