Skip to content
Advertisement

“Database ‘docker-db’ does not exist”

so I have this problem building django/postgre app with docker. it keeps saying “database does not exist” and quite few time googling doesnt bring any results. I already scrapped the volumes and rebuilt my app few times, followed different guides on how people do the postgres, still no luck making it work for me. Dockerfile

FROM python:3.9.6-alpine
WORKDIR /blog-master

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install psycopg2 dependencies
RUN apk update 
    && apk add postgresql-dev gcc python3-dev musl-dev

RUN pip install --upgrade pip
RUN apk update 
    && apk add postgresql-dev gcc python3-dev musl-dev
    
COPY requirements.txt .
RUN pip install -r requirements.txt

COPY . .


compose.yml

services:
  back:
    image: python:3.9
    build: ./blog-master
    command:  sh -c "python manage.py runserver 0.0.0.0:8000 &&
              /usr/local/bin/gunicorn blog.wsgi:application -w 2 -b :8000"
    expose:
      - "8000"

    volumes:
      - blog-master-django:/usr/src/app
      - blog-master-static:/usr/src/app/static
    env_file: .env
    links:
      - db
    depends_on:
      db:
        condition: service_healthy
  
  db:
    image: postgres:14
    restart: always
    expose:
      - "5432"
    environment:
      - POSTGRES_DB=docker_db
      - POSTGRES_USER=docker
      - POSTGRES_PASSWORD=docker
    ports:
      - "5432:5432"
    volumes:
      - pgdata:/var/lib/postgresql/data/
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U docker"]
      interval: 5s
      timeout: 5s
      retries: 5

volumes:
  blog-master-django:
  blog-master-static:
  pgdata:

.env

DEBUG=1
SECRET_KEY='**key-here**'
DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1]
SQL_ENGINE=django.db.backends.postgresql
SQL_DATABASE=docker_db
SQL_USER=docker
SQL_PASSWORD=docker
SQL_HOST=db
SQL_PORT=5432

UPDATE: Error messages starts with

db_1    | 2022-09-29 15:33:58.432 UTC [1] LOG:  database system is ready to accept connections
db_1    | 2022-09-29 15:34:03.467 UTC [40] FATAL:  database "docker" does not exist

and then my django starts spamming the same

back_1  | psycopg2.OperationalError: FATAL:  database "docker_db" does not exist

but the weird thing is, then it proceeds giving same errors from db, but

db_1    | 2022-09-29 15:34:23.801 UTC [73] FATAL:  database "docker" does not exist

this time it says “database ‘docker'” but I dont have DB name “docker” used in my code

Full error:

Recreating blog-showcase_db_1 ... done
Recreating blog-showcase_back_1 ... done
Attaching to blog-showcase_db_1, blog-showcase_back_1
db_1    | 
db_1    | PostgreSQL Database directory appears to contain a database; Skipping initialization
db_1    | 
db_1    | 2022-09-29 15:33:58.392 UTC [1] LOG:  starting PostgreSQL 14.5 (Debian 14.5-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
db_1    | 2022-09-29 15:33:58.393 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1    | 2022-09-29 15:33:58.393 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1    | 2022-09-29 15:33:58.399 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1    | 2022-09-29 15:33:58.416 UTC [26] LOG:  database system was shut down at 2022-09-29 15:33:44 UTC
db_1    | 2022-09-29 15:33:58.432 UTC [1] LOG:  database system is ready to accept connections
db_1    | 2022-09-29 15:34:03.467 UTC [40] FATAL:  database "docker" does not exist
back_1  | Watching for file changes with StatReloader
back_1  | Performing system checks...
back_1  | 
back_1  | System check identified no issues (0 silenced).
db_1    | 2022-09-29 15:34:05.823 UTC [41] FATAL:  database "docker_db" does not exist
back_1  | Exception in thread django-main-thread:
back_1  | Traceback (most recent call last):
back_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 244, in ensure_connection
back_1  |     self.connect()
back_1  |   File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
back_1  |     return func(*args, **kwargs)
back_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 225, in connect
back_1  |     self.connection = self.get_new_connection(conn_params)
back_1  |   File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
back_1  |     return func(*args, **kwargs)
back_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 203, in get_new_connection
back_1  |     connection = Database.connect(**conn_params)
back_1  |   File "/usr/local/lib/python3.9/site-packages/psycopg2/__init__.py", line 122, in connect
back_1  |     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
back_1  | psycopg2.OperationalError: FATAL:  database "docker_db" does not exist
back_1  | 
back_1  | 
back_1  | The above exception was the direct cause of the following exception:
back_1  | 
back_1  | Traceback (most recent call last):
back_1  |   File "/usr/local/lib/python3.9/threading.py", line 973, in _bootstrap_inner
back_1  |     self.run()
back_1  |   File "/usr/local/lib/python3.9/threading.py", line 910, in run
back_1  |     self._target(*self._args, **self._kwargs)
back_1  |   File "/usr/local/lib/python3.9/site-packages/django/utils/autoreload.py", line 64, in wrapper
back_1  |     fn(*args, **kwargs)
back_1  |   File "/usr/local/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 137, in inner_run
back_1  |     self.check_migrations()
back_1  |   File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 576, in check_migrations
back_1  |     executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
back_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/migrations/executor.py", line 18, in __init__
back_1  |     self.loader = MigrationLoader(self.connection)
back_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/migrations/loader.py", line 58, in __init__
back_1  |     self.build_graph()
back_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/migrations/loader.py", line 235, in build_graph
back_1  |     self.applied_migrations = recorder.applied_migrations()
back_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/migrations/recorder.py", line 81, in applied_migrations
back_1  |     if self.has_table():
back_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/migrations/recorder.py", line 57, in has_table
back_1  |     with self.connection.cursor() as cursor:
back_1  |   File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
back_1  |     return func(*args, **kwargs)
back_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 284, in cursor
back_1  |     return self._cursor()
back_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 260, in _cursor
back_1  |     self.ensure_connection()
back_1  |   File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
back_1  |     return func(*args, **kwargs)
back_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 244, in ensure_connection
back_1  |     self.connect()
back_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/utils.py", line 91, in __exit__
back_1  |     raise dj_exc_value.with_traceback(traceback) from exc_value
back_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 244, in ensure_connection
back_1  |     self.connect()
back_1  |   File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
back_1  |     return func(*args, **kwargs)
back_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 225, in connect
back_1  |     self.connection = self.get_new_connection(conn_params)
back_1  |   File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
back_1  |     return func(*args, **kwargs)
back_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 203, in get_new_connection
back_1  |     connection = Database.connect(**conn_params)
back_1  |   File "/usr/local/lib/python3.9/site-packages/psycopg2/__init__.py", line 122, in connect
back_1  |     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
back_1  | django.db.utils.OperationalError: FATAL:  database "docker_db" does not exist
back_1  | 
db_1    | 2022-09-29 15:34:08.544 UTC [49] FATAL:  database "docker" does not exist
db_1    | 2022-09-29 15:34:13.628 UTC [57] FATAL:  database "docker" does not exist
db_1    | 2022-09-29 15:34:18.720 UTC [65] FATAL:  database "docker" does not exist
db_1    | 2022-09-29 15:34:23.801 UTC [73] FATAL:  database "docker" does not exist
^CGracefully stopping... (press Ctrl+C again to force)
Stopping blog-showcase_back_1   ... done
Stopping blog-showcase_db_1     ... done

Advertisement

Answer

So, the answer was pretty simple and pretty “I don’t know what was that”.

First thing, I forgot to add DATABASE=postgres in my .env file, which was crucial for Django to notice the needed db

The problem with db searching for not-asked-for db? No idea, I just renamed needed db to “docker”, which my db service looked for, and it worked.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement