django docker postgres issue: password authentication failed

  database, django, docker, postgresql, python

I might be the n person to ask this question but i am sorry because now i spent 5 hours on it and still has not solve it

Everything was working great in my django project until i messed around with multi user setup…thats another

now postgres is giving me a hard time to work while using docker-compose

.env file

POSTGRES_NAME=somedb
POSTGRES_USER=someuser
POSTGRES_PASSWORD=somepassword

django settings

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': os.environ.get("POSTGRES_NAME"),
        'USER': os.environ.get("POSTGRES_USER"),
        'PASSWORD': os.environ.get("POSTGRES_PASSWORD"),
        'HOST': 'db',
        'PORT': '5432',
    }
}

docker-compose

version: "3.9"

services:
  backend:
    build: ./backend
    container_name: backend
    stdin_open: true
    tty: true
    restart: "on-failure"
    env_file: .env
    volumes:
      - ./backend:/backend
      - ./backend/static:/backend/static
    ports:
      - 8000:8000
    depends_on:
      - db
    links:
      - db:db
    networks:
      - "backend_network"
  db:
    image: postgres
    container_name: db
    restart: always
    env_file: .env
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    ports:
      - 5432:5432
    networks:
      - "backend_network"

networks:
  backend_network:
    driver: bridge

volumes:
  postgres_data:

when i spin up the containers with docker-compose -d --build

and check the logs with docker-compose logs, here is what i see

backend     |   File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
backend     |     return func(*args, **kwargs)
backend     |   File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 200, in connect
backend     |     self.connection = self.get_new_connection(conn_params)
backend     |   File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
backend     |     return func(*args, **kwargs)
backend     |   File "/usr/local/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 187, in get_new_connection
backend     |     connection = Database.connect(**conn_params)
backend     |   File "/usr/local/lib/python3.8/site-packages/psycopg2/__init__.py", line 122, in connect
backend     |     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
backend     | django.db.utils.OperationalError: FATAL:  password authentication failed for user "someuser"

The DB is crashing and i tried pretty much everything inside psql postgres in the terminal, alter password og that user password, grant full privileges to that database.

Still not working

If anyone has a suggestion or know how to solve it will be very helpful thanks

Source: Docker Questions

LEAVE A COMMENT