Unable to run Django with PostgreSQL in Docker
I wanted to set up a Django app with PostgresDb inside docker containers so that's why I wanted to setup docker-compose but when I execute my code docker, django and db all are working fine and I also developed some API's and they were also working fine as expected but unfortunately, suddenly I'm blocked with these errors:
pgdb_1 |
pgdb_1 | 2021-12-11 15:05:38.674 UTC [1] LOG: starting PostgreSQL 14.1 (Debian 14.1-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
pgdb_1 | 2021-12-11 15:05:38.674 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
pgdb_1 | 2021-12-11 15:05:38.674 UTC [1] LOG: listening on IPv6 address "::", port 5432
pgdb_1 | 2021-12-11 15:05:38.697 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
pgdb_1 | 2021-12-11 15:05:38.729 UTC [27] LOG: database system was shut down at 2021-12-11 15:03:09 UTC
pgdb_1 | 2021-12-11 15:05:38.761 UTC [1] LOG: database system is ready to accept connections
django_1 | Watching for file changes with StatReloader
django_1 | Performing system checks...
django_1 |
django_1 | System check identified no issues (0 silenced).
pgdb_1 | 2021-12-11 15:05:41.390 UTC [34] FATAL: password authentication failed for user "postgres"
pgdb_1 | 2021-12-11 15:05:41.390 UTC [34] DETAIL: Role "postgres" does not exist.
pgdb_1 | Connection matched pg_hba.conf line 100: "host all all all scram-sha-256"
django_1 | Exception in thread django-main-thread:
django_1 | Traceback (most recent call last):
django_1 | File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
django_1 | self.connect()
django_1 | File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 33, in inner
django_1 | return func(*args, **kwargs)
django_1 | File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 200, in connect
django_1 | self.connection = self.get_new_connection(conn_params)
django_1 | File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 33, in inner
django_1 | return func(*args, **kwargs)
django_1 | File "/usr/local/lib/python3.10/site-packages/django/db/backends/postgresql/base.py", line 187, in get_new_connection
django_1 | connection = Database.connect(**conn_params)
django_1 | File "/usr/local/lib/python3.10/site-packages/psycopg2/__init__.py", line 122, in connect
django_1 | conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django_1 | psycopg2.OperationalError: FATAL: password authentication failed for user "postgres"
django_1 |
django_1 |
django_1 | The above exception was the direct cause of the following exception:
django_1 |
django_1 | Traceback (most recent call last):
django_1 | File "/usr/local/lib/python3.10/threading.py", line 1009, in _bootstrap_inner
django_1 | self.run()
django_1 | File "/usr/local/lib/python3.10/threading.py", line 946, in run
django_1 | self._target(*self._args, **self._kwargs)
django_1 | File "/usr/local/lib/python3.10/site-packages/django/utils/autoreload.py", line 64, in wrapper
django_1 | fn(*args, **kwargs)
django_1 | File "/usr/local/lib/python3.10/site-packages/django/core/management/commands/runserver.py", line 121, in inner_run
django_1 | self.check_migrations()
django_1 | File "/usr/local/lib/python3.10/site-packages/django/core/management/base.py", line 486, in check_migrations
django_1 | executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
django_1 | File "/usr/local/lib/python3.10/site-packages/django/db/migrations/executor.py", line 18, in __init__
django_1 | self.loader = MigrationLoader(self.connection)
django_1 | File "/usr/local/lib/python3.10/site-packages/django/db/migrations/loader.py", line 53, in __init__
django_1 | self.build_graph()
django_1 | File "/usr/local/lib/python3.10/site-packages/django/db/migrations/loader.py", line 220, in build_graph
django_1 | self.applied_migrations = recorder.applied_migrations()
django_1 | File "/usr/local/lib/python3.10/site-packages/django/db/migrations/recorder.py", line 77, in applied_migrations
django_1 | if self.has_table():
django_1 | File "/usr/local/lib/python3.10/site-packages/django/db/migrations/recorder.py", line 55, in has_table
django_1 | with self.connection.cursor() as cursor:
django_1 | File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 33, in inner
django_1 | return func(*args, **kwargs)
django_1 | File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 259, in cursor
django_1 | return self._cursor()
django_1 | File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 235, in _cursor
django_1 | self.ensure_connection()
django_1 | File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 33, in inner
django_1 | return func(*args, **kwargs)
django_1 | File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 218, in ensure_connection
django_1 | with self.wrap_database_errors:
django_1 | File "/usr/local/lib/python3.10/site-packages/django/db/utils.py", line 90, in __exit__
django_1 | raise dj_exc_value.with_traceback(traceback) from exc_value
django_1 | File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
django_1 | self.connect()
django_1 | File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 33, in inner
django_1 | return func(*args, **kwargs)
django_1 | File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 200, in connect
django_1 | self.connection = self.get_new_connection(conn_params)
django_1 | File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 33, in inner
django_1 | return func(*args, **kwargs)
django_1 | File "/usr/local/lib/python3.10/site-packages/django/db/backends/postgresql/base.py", line 187, in get_new_connection
django_1 | connection = Database.connect(**conn_params)
django_1 | File "/usr/local/lib/python3.10/site-packages/psycopg2/__init__.py", line 122, in connect
django_1 | conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django_1 | django.db.utils.OperationalError: FATAL: password authentication failed for user "postgres"
My Dockerfile
FROM python:3
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
WORKDIR /code
RUN pip install --upgrade pip
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/
docker-compose.yml
version: "3.8"
services:
pgdb:
image: "postgres"
restart: always
volumes:
- ./data/db:/var/lib/postgresql/data
ports:
- "5432:5432"
environment:
- POSTGRES_DB=digipm
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
django:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- pgdb
Database config settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'digipm',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': 'pgdb',
'PORT': 5432,
}
}