Django.db.utils.OperationalError connection is bad

I am trying to use python manage.py runserver to show my web page and render it. Django for Professionals says to use Docker. I run the python manage.py runserver command and get this error message.

Error Message

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/threading.py", line 1038, in _bootstrap_inner
    self.run()
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/threading.py", line 975, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/andrewstribling/dev/code/ch4-bookstore/project_env/lib/python3.11/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/Users/andrewstribling/dev/code/ch4-bookstore/project_env/lib/python3.11/site-packages/django/core/management/commands/runserver.py", line 136, in inner_run
    self.check_migrations()
  File "/Users/andrewstribling/dev/code/ch4-bookstore/project_env/lib/python3.11/site-packages/django/core/management/base.py", line 574, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/andrewstribling/dev/code/ch4-bookstore/project_env/lib/python3.11/site-packages/django/db/migrations/executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/andrewstribling/dev/code/ch4-bookstore/project_env/lib/python3.11/site-packages/django/db/migrations/loader.py", line 58, in __init__
    self.build_graph()
  File "/Users/andrewstribling/dev/code/ch4-bookstore/project_env/lib/python3.11/site-packages/django/db/migrations/loader.py", line 235, in build_graph
    self.applied_migrations = recorder.applied_migrations()
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/andrewstribling/dev/code/ch4-bookstore/project_env/lib/python3.11/site-packages/django/db/migrations/recorder.py", line 89, in applied_migrations
    if self.has_table():
       ^^^^^^^^^^^^^^^^
  File "/Users/andrewstribling/dev/code/ch4-bookstore/project_env/lib/python3.11/site-packages/django/db/migrations/recorder.py", line 63, in has_table
    with self.connection.cursor() as cursor:
         ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/andrewstribling/dev/code/ch4-bookstore/project_env/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/andrewstribling/dev/code/ch4-bookstore/project_env/lib/python3.11/site-packages/django/db/backends/base/base.py", line 316, in cursor
    return self._cursor()
           ^^^^^^^^^^^^^^
  File "/Users/andrewstribling/dev/code/ch4-bookstore/project_env/lib/python3.11/site-packages/django/db/backends/base/base.py", line 292, in _cursor
    self.ensure_connection()
  File "/Users/andrewstribling/dev/code/ch4-bookstore/project_env/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/andrewstribling/dev/code/ch4-bookstore/project_env/lib/python3.11/site-packages/django/db/backends/base/base.py", line 274, in ensure_connection
    with self.wrap_database_errors:
  File "/Users/andrewstribling/dev/code/ch4-bookstore/project_env/lib/python3.11/site-packages/django/db/utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/Users/andrewstribling/dev/code/ch4-bookstore/project_env/lib/python3.11/site-packages/django/db/backends/base/base.py", line 275, in ensure_connection
    self.connect()
  File "/Users/andrewstribling/dev/code/ch4-bookstore/project_env/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/andrewstribling/dev/code/ch4-bookstore/project_env/lib/python3.11/site-packages/django/db/backends/base/base.py", line 256, in connect
    self.connection = self.get_new_connection(conn_params)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/andrewstribling/dev/code/ch4-bookstore/project_env/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/andrewstribling/dev/code/ch4-bookstore/project_env/lib/python3.11/site-packages/django/db/backends/postgresql/base.py", line 275, in get_new_connection
    connection = self.Database.connect(**conn_params)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/andrewstribling/dev/code/ch4-bookstore/project_env/lib/python3.11/site-packages/psycopg/connection.py", line 750, in connect
    raise last_ex.with_traceback(None)
django.db.utils.OperationalError: connection is bad: nodename nor servname provided, or not known

I run these Docker commands and python manage.py runserver

“$ docker-compose down
$ docker-compose up -d”

What have I tried and what am I expecting?

I checked stackoverflow and they said to turn on postgress which I did but there is no change in the error.

I have run a search in django forum for the error message but have not found anything.

I checked the django docs and it just says that this error is treated in a very standard way.
Django Docs Link: Django Exceptions | Django documentation | Django

Can someone explain where I can change the nodename or serv name?

The abbreviated error message is

django.db.utils.OperationalError: connection is bad: nodename nor servname provided, or not known

settings.py

# django_project/settings.py
DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": "postgres",
        "USER": "postgres",
        "PASSWORD": "postgres",
        "HOST": "db",  # set in docker-compose.yml
        "PORT": 5432,  # default postgres port
    }
}

I have double checked the instruction inside of Django For Professionals and it matches what I have input into visual studio code.
The abbreviated error message is

django.db.utils.OperationalError: connection is bad: nodename nor servname provided, or not known

docker-compose.yml

version: "3.9"

services:
  web:
    build: .
    command: python /code/manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - 8000:8000
    depends_on:
      - db
  db:
    image: postgres:13
    volumes:
      - postgres_data:/var/lib/postgresql/data/
  environment:
    - "POSTGRES_HOST_AUTH_METHOD=trust"

volumes:
  postgres_data:

Dockerfile

# Pull base image
FROM python
# Set environment variables
ENV PIP_DISABLE_PIP_VERSION_CHECK 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# Set work directory
WORKDIR /code
# Install dependencies
COPY ./requirements.txt .
RUN pip install -r requirements.txt
# Copy project
COPY . .

My postgress settings is showing the username set to postgres and the password set to postgres:

My docker compose yml file seems to have the command python manage.py runserver 8000 could that have something to do with it?

version: "3.9"

services:
  web:
    build: .
    command: python /code/manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - 8000:8000
    depends_on:
      - db
  db:
    image: postgres:13
    volumes:
      - postgres_data:/var/lib/postgresql/data/
  environment:
    - "POSTGRES_HOST_AUTH_METHOD=trust"

volumes:
  postgres_data:

Perhaps I have found the issue. I have tried to access my database via pg admin. The password I have established in settings.py in my django project will not work for pg admin.

I get this error I tried opening pgadmin to access postgress and the password for my database that was established in settings.py is not working. how can this be?

the error says connection failed 1) port 5432 failed: could not recieve data from server. Connection failed.

how can this be? 5432 is the default port of postgress.

GIthub Link: GitHub - strikeouts27/django_for_professionals_bookstore

I turned on the containers and Postgress and it is running now for some reason where before it was not. Moving forward all solutions need to have postgress fully up with the pg admin. Also all containers need to be active pertaining to the project along with their images.



I worked with someone and they told me that I needed to have my postgress database up and running completly before trying to dockerize anything. So he made a new server for my postgress database via object register server and made a new connection point using an ip address. I belive that was one that started with 127.0.0.0. He went into django_projects settings.py an added that ip address into allowed hosts. He checked my database settigns and looked at the Host variable inside the DATABASE section settings.py he also updated the PORT settings. he pointed out to me that having the same port number of 5432 for the container and for postgress was wrong. he told me to change the port to 5431 inside my django project. Also the username and password were changed in django project settings.py and when the new database was set up in postgress with the new server we utilized the username and password inside of that django projects settings.py file had. we made sure to have matching information.

Looking up in the documentation how the respective parts of django work and how the respective parts of pg admin works and even looking up the docker compose file parts seems to be the way to go.

Okay so I opened up pg admin and tried to create a server or a connection point and I inputted the same information as in settings.py still getting errors for some reason…

okay I went into pg admin and created a database under the same name as the one in django settings.py. After doing that, I ran the make migrations and migrate command and it looks like the migrations migrated into the empty database! I ran python3 manage.py runserver and it worked! YEAH! ALRIGHT!