# Django.db.utils.OperationalError connection is bad

**URL:** <https://forum.djangoproject.com/t/django-db-utils-operationalerror-connection-is-bad/26495>\
**Category:** Deployment\
**Created:** [December 29, 2023, 4:24am UTC](https://forum.djangoproject.com/t/django-db-utils-operationalerror-connection-is-bad/26495 "2023-12-29T04:24:21Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![strikeouts27](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/strikeouts27/32/13899_2.png) [@strikeouts27](https://forum.djangoproject.com/u/strikeouts27)\
**Post date:** [December 29, 2023, 4:24am UTC](https://forum.djangoproject.com/t/django-db-utils-operationalerror-connection-is-bad/26495/1 "2023-12-29T04:24:21Z")

</div>

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

```auto
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

```auto
“$ 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](https://docs.djangoproject.com/en/5.0/ref/exceptions/#django.db.OperationalError)

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

---

<div class="post-metadata">

**Author:** ![strikeouts27](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/strikeouts27/32/13899_2.png) [@strikeouts27](https://forum.djangoproject.com/u/strikeouts27)\
**Post date:** [January 3, 2024, 7:50am UTC](https://forum.djangoproject.com/t/django-db-utils-operationalerror-connection-is-bad/26495/7 "2024-01-03T07:50:26Z")

</div>

The abbreviated error message is

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

```

settings.py

```auto
# 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
    }
}

```

---

<div class="post-metadata">

**Author:** ![strikeouts27](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/strikeouts27/32/13899_2.png) [@strikeouts27](https://forum.djangoproject.com/u/strikeouts27)\
**Post date:** [January 3, 2024, 8:12am UTC](https://forum.djangoproject.com/t/django-db-utils-operationalerror-connection-is-bad/26495/8 "2024-01-03T08:12:38Z")

</div>

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

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

```

docker-compose.yml

```auto
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

```auto
# 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:

---

<div class="post-metadata">

**Author:** ![strikeouts27](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/strikeouts27/32/13899_2.png) [@strikeouts27](https://forum.djangoproject.com/u/strikeouts27)\
**Post date:** [January 3, 2024, 8:36am UTC](https://forum.djangoproject.com/t/django-db-utils-operationalerror-connection-is-bad/26495/10 "2024-01-03T08:36:57Z")

</div>

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

```auto
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:

```

---

<div class="post-metadata">

**Author:** ![strikeouts27](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/strikeouts27/32/13899_2.png) [@strikeouts27](https://forum.djangoproject.com/u/strikeouts27)\
**Post date:** [January 7, 2024, 12:24pm UTC](https://forum.djangoproject.com/t/django-db-utils-operationalerror-connection-is-bad/26495/12 "2024-01-07T12:24:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![strikeouts27](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/strikeouts27/32/13899_2.png) [@strikeouts27](https://forum.djangoproject.com/u/strikeouts27)\
**Post date:** [January 7, 2024, 12:54pm UTC](https://forum.djangoproject.com/t/django-db-utils-operationalerror-connection-is-bad/26495/13 "2024-01-07T12:54:51Z")

</div>

GIthub Link: [GitHub - strikeouts27/django\_for\_professionals\_bookstore](https://github.com/strikeouts27/django_for_professionals_bookstore)

---

<div class="post-metadata">

**Author:** ![strikeouts27](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/strikeouts27/32/13899_2.png) [@strikeouts27](https://forum.djangoproject.com/u/strikeouts27)\
**Post date:** [January 8, 2024, 12:57am UTC](https://forum.djangoproject.com/t/django-db-utils-operationalerror-connection-is-bad/26495/14 "2024-01-08T00:57:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![strikeouts27](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/strikeouts27/32/13899_2.png) [@strikeouts27](https://forum.djangoproject.com/u/strikeouts27)\
**Post date:** [January 9, 2024, 11:52am UTC](https://forum.djangoproject.com/t/django-db-utils-operationalerror-connection-is-bad/26495/15 "2024-01-09T11:52:59Z")

</div>

![Screen Shot 2024-01-09 at 5.52.11 AM](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/4/d/4d221fdcc3fc6f8637028c5218a7acb758eb402b.jpeg)  
 ![Screen Shot 2024-01-09 at 5.52.20 AM](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/0/9/09bd66af546241b753c591bb7b60f61a4cfe79bc.jpeg)  
 ![Screen Shot 2024-01-09 at 5.52.26 AM](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/5/0/500d18b4ea0edc6f9143e5369816ffe57c4eff21.jpeg)

---

<div class="post-metadata">

**Author:** ![strikeouts27](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/strikeouts27/32/13899_2.png) [@strikeouts27](https://forum.djangoproject.com/u/strikeouts27)\
**Post date:** [January 12, 2024, 1:17pm UTC](https://forum.djangoproject.com/t/django-db-utils-operationalerror-connection-is-bad/26495/16 "2024-01-12T13:17:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![strikeouts27](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/strikeouts27/32/13899_2.png) [@strikeouts27](https://forum.djangoproject.com/u/strikeouts27)\
**Post date:** [February 14, 2024, 7:20am UTC](https://forum.djangoproject.com/t/django-db-utils-operationalerror-connection-is-bad/26495/17 "2024-02-14T07:20:09Z")

</div>

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!
