2

I am trying docker-compose an app with django and postgres using docker-compose but I get an error with the "NAME"

Here is my settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'postgres',
        'USER': 'postgres',
        'PASSWORD': 'postgres',
        'HOST': 'db',
        'PORT': 5432,
    }
}

Here is my docker-compose

version: "3.9"

services:
  db:
    restart: always
    image: postgres:latest
    ports:
      - "5432:5432"
    volumes:
      - ./data/db:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
  web:
    restart: always
    build: ./backend
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/usr/src/app
    ports:
      - "8000:8000"
    depends_on:
      - db

django.db.utils.OperationalError: FATAL: password authentication failed for user "postgres"

Thanks for your help

2
  • Can you connect manually to your postgres database with this password? Commented Sep 30, 2021 at 11:19
  • @Yaroslav thanks man, your comment made me think in a different angle and I managed to fix it! Baaah, that was alot of work. I addd my answer Commented Sep 30, 2021 at 12:15

1 Answer 1

0

Lots of things were wrong in my code. After 6 hours of debugging.

First I changed all my docker-compose.yml to

version: "3.9"
services:
  db:
    image: postgres
    volumes:
      - ./data/db:/var/lib/postgresql/data

  web:
    build: ./backend
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/usr/src/app
    ports:
      - "8000:8000"
    depends_on:
      - db

In my settings.py I did:

import environ
import os

env = environ.Env(
    # set casting, default value
    DEBUG=(bool, False)
)

# Set the project base directory
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Take environment variables from .env file
environ.Env.read_env(os.path.join(BASE_DIR, '.env'))

# False if not in os.environ because of casting above
DEBUG = env('DEBUG')

# Raises Django's ImproperlyConfigured
# exception if SECRET_KEY not in os.environ
SECRET_KEY = env('SECRET_KEY')

# Parse database connection url strings
# like psql://user:[email protected]:8458/db
DATABASES = {
    # read os.environ['DATABASE_URL'] and raises
    # ImproperlyConfigured exception if not found
    #
    # The db() method is an alias for db_url().
    'default': env.db(),


}

I then created the .env

DEBUG=on
SECRET_KEY=xxx
DATABASE_URL=psql://postgres:123@db:5432/postgres

I then entered into docker using

docker ps -a

docker exec -it <container_id> bash

psql postgres postgres

\password postgres

123
123

I then migrated my database

docker-compose exec web python manage.py makemigrations
docker-compose exec web python manage.py migrate
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.