title: Is the server running on host "db" (172.22.0.2) and accepting TCP/IP connections on port 5432?
body: <p>I got my docker compose with postgres and django. Nevertheless when trying to build i am receiving the error below. How can i fix it?</p>
  <pre><code>Is the server running on host &quot;db&quot; (172.22.0.2) and accepting TCP/IP connections on port 5432?
  </code></pre>
  <p><strong>Log containing the error:</strong></p>
  <pre><code>web_1  | Watching for file changes with StatReloader
  web_1  | Performing system checks...
  web_1  |
  web_1  | System check identified no issues (0 silenced).
  web_1  | Exception in thread django-main-thread:
  web_1  | Traceback (most recent call last):
  web_1  |   File &quot;/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py&quot;, line 219, in ensure_connection
  web_1  |     self.connect()
  web_1  |   File &quot;/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py&quot;, line 33, in inner
  web_1  |     return func(*args, **kwargs)
  web_1  |   File &quot;/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py&quot;, line 200, in connect
  web_1  |     self.connection = self.get_new_connection(conn_params)
  web_1  |   File &quot;/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py&quot;, line 33, in inner
  web_1  |     return func(*args, **kwargs)
  web_1  |   File &quot;/usr/local/lib/python3.10/site-packages/django/db/backends/postgresql/base.py&quot;, line 187, in get_new_connection
  web_1  |     connection = Database.connect(**conn_params)
  web_1  |   File &quot;/usr/local/lib/python3.10/site-packages/psycopg2/__init__.py&quot;, line 122, in connect
  web_1  |     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
  web_1  | psycopg2.OperationalError: could not connect to server: Connection refused
  web_1  |        Is the server running on host &quot;db&quot; (172.22.0.2) and accepting
  web_1  |        TCP/IP connections on port 5432?
  web_1  |
  web_1  |
  web_1  | The above exception was the direct cause of the following exception:
  web_1  |
  web_1  | Traceback (most recent call last):
  web_1  |   File &quot;/usr/local/lib/python3.10/threading.py&quot;, line 1009, in _bootstrap_inner
  web_1  |     self.run()
  web_1  |   File &quot;/usr/local/lib/python3.10/threading.py&quot;, line 946, in run
  web_1  |     self._target(*self._args, **self._kwargs)
  web_1  |   File &quot;/usr/local/lib/python3.10/site-packages/django/utils/autoreload.py&quot;, line 64, in wrapper
  web_1  |     fn(*args, **kwargs)
  web_1  |   File &quot;/usr/local/lib/python3.10/site-packages/django/core/management/commands/runserver.py&quot;, line 121, in inner_run
  web_1  |     self.check_migrations()
  web_1  |   File &quot;/usr/local/lib/python3.10/site-packages/django/core/management/base.py&quot;, line 486, in check_migrations
  web_1  |     executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  web_1  |   File &quot;/usr/local/lib/python3.10/site-packages/django/db/migrations/executor.py&quot;, line 18, in __init__
  web_1  |     self.loader = MigrationLoader(self.connection)
  web_1  |   File &quot;/usr/local/lib/python3.10/site-packages/django/db/migrations/loader.py&quot;, line 53, in __init__
  web_1  |     self.build_graph()
  web_1  |   File &quot;/usr/local/lib/python3.10/site-packages/django/db/migrations/loader.py&quot;, line 220, in build_graph
  web_1  |     self.applied_migrations = recorder.applied_migrations()
  web_1  |   File &quot;/usr/local/lib/python3.10/site-packages/django/db/migrations/recorder.py&quot;, line 77, in applied_migrations
  web_1  |     if self.has_table():
  web_1  |   File &quot;/usr/local/lib/python3.10/site-packages/django/db/migrations/recorder.py&quot;, line 55, in has_table
  web_1  |     with self.connection.cursor() as cursor:
  web_1  |   File &quot;/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py&quot;, line 33, in inner
  web_1  |     return func(*args, **kwargs)
  web_1  |   File &quot;/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py&quot;, line 259, in cursor
  web_1  |     return self._cursor()
  web_1  |   File &quot;/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py&quot;, line 235, in _cursor
  web_1  |     self.ensure_connection()
  web_1  |   File &quot;/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py&quot;, line 33, in inner
  web_1  |     return func(*args, **kwargs)
  web_1  |   File &quot;/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py&quot;, line 218, in ensure_connection
  web_1  |     with self.wrap_database_errors:
  web_1  |   File &quot;/usr/local/lib/python3.10/site-packages/django/db/utils.py&quot;, line 90, in __exit__
  web_1  |     raise dj_exc_value.with_traceback(traceback) from exc_value
  web_1  |   File &quot;/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py&quot;, line 219, in ensure_connection
  web_1  |     self.connect()
  web_1  |   File &quot;/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py&quot;, line 33, in inner
  web_1  |     return func(*args, **kwargs)
  web_1  |   File &quot;/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py&quot;, line 200, in connect
  web_1  |     self.connection = self.get_new_connection(conn_params)
  web_1  |   File &quot;/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py&quot;, line 33, in inner
  web_1  |     return func(*args, **kwargs)
  web_1  |   File &quot;/usr/local/lib/python3.10/site-packages/django/db/backends/postgresql/base.py&quot;, line 187, in get_new_connection
  web_1  |     connection = Database.connect(**conn_params)
  web_1  |   File &quot;/usr/local/lib/python3.10/site-packages/psycopg2/__init__.py&quot;, line 122, in connect
  web_1  |     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
  web_1  | django.db.utils.OperationalError: could not connect to server: Connection refused
  web_1  |        Is the server running on host &quot;db&quot; (172.22.0.2) and accepting
  web_1  |        TCP/IP connections on port 5432?
  web_1  |
  db_1   | running bootstrap script ... ok
  db_1   | performing post-bootstrap initialization ... ok
  db_1   | syncing data to disk ... ok
  db_1   |
  db_1   | initdb: warning: enabling &quot;trust&quot; authentication for local connections
  db_1   | You can change this by editing pg_hba.conf or using the option -A, or
  db_1   | --auth-local and --auth-host, the next time you run initdb.
  db_1   |
  db_1   | Success. You can now start the database server using:
  db_1   |
  db_1   |     pg_ctl -D /var/lib/postgresql/data -l logfile st
  </code></pre>
  <p><strong>docker-compose.yaml:</strong></p>
  <pre><code>version: &quot;3.9&quot;
     
  services:
    db:
      image: postgres
      volumes:
        - ./data/db:/var/lib/postgresql/data
      environment:
        - POSTGRES_PASSWORD=postgres
    web:
      build: .
      command: python manage.py runserver 0.0.0.0:8000
      volumes:
        - .:/code
      ports:
        - &quot;8000:8000&quot;
      environment:
        - POSTGRES_NAME=postgres
        - POSTGRES_USER=postgres
        - POSTGRES_PASSWORD=postgres
      depends_on:
        - db
  </code></pre>
  <p><strong>dockerfile</strong></p>
  <pre class="lang-sh prettyprint-override"><code># syntax=docker/dockerfile:1
  FROM python:3
  ENV PYTHONDONTWRITEBYTECODE=1
  ENV PYTHONUNBUFFERED=1
  WORKDIR /code
  COPY requirements.txt /code/
  RUN pip install -r requirements.txt
  COPY . /code/
  </code></pre>