Unable to run Django with PostgreSQL in Docker

Unable to run Django with PostgreSQL in Docker

Questions : Unable to run Django with PostgreSQL in Docker

591

I wanted to set up a Django app with in4codes_django PostgresDb inside docker containers so in4codes_django that’s why I wanted to setup docker-compose in4codes_django but when I execute my code docker, django in4codes_django and db all are working fine and I also in4codes_django developed some API’s and they were also in4codes_django working fine as expected but unfortunately, in4codes_django 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, } }

Total Answers 1
33

Answers 1 : of Unable to run Django with PostgreSQL in Docker

I ran into a problem where django isn’t in4codes_django connecting to the database as I was in4codes_django trying to create a new django in4codes_django developement environment using docker. I in4codes_django created a new project docker-compose run in4codes_django web django-admin startproject django_src in4codes_django . then docker-compose up and behold it in4codes_django ain’t connecting to the database.

I solved the problem after adding in4codes_django environment configuration in in4codes_django docker-compose on both the database and in4codes_django Django itself (See code snippets below). in4codes_django Then I made use of os.environ.get(”) in4codes_django for host, name, user & password to in4codes_django give definite references to and fro in4codes_django docker-compose environment in4codes_django configuration.

Then I ran the following:

docker-compose run –rm app python in4codes_django manage.py migrate

docker-compose run –rm app python in4codes_django manage.py createsuperuser

Creating docker-django-postgres_app_run in4codes_django … done Username (leave blank to use in4codes_django ‘user’): Email in4codes_django address: Password: Password in4codes_django (again): This password is too in4codes_django common. Bypass password validation and in4codes_django create user anyway? [y/N]: y Superuser in4codes_django created successfully.

Dockerfile

FROM python:3 ENV PYTHONDONTWRITEBYTECODE=1 ENV PYTHONUNBUFFERED=1 WORKDIR /code COPY requirements.txt /code/ RUN pip install -r requirements.txt COPY . /code/

docker-composefile

version: "3.9" services: db: image: postgres volumes: - ./data/db:/var/lib/postgresql/data environment: - POSTGRES_NAME=postgres - POSTGRES_USER=postgres - POSTGRES_PASSWORD=postgres web: build: . command: python manage.py runserver 0.0.0.0:8000 volumes: - .:/code ports: - "8000:8000" environment: - DB_HOST=db - POSTGRES_NAME=postgres - POSTGRES_USER=postgres - POSTGRES_PASSWORD=postgres depends_on: - db

settings.py

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

requirements.txt

asgiref==3.4.1 Django==4.0.1 psycopg2==2.9.3 sqlparse==0.4.2

0