Seite 1 von 1

django.db.utils.OperationalError: could not connect to server: Connection refused

Verfasst: Sonntag 23. Mai 2021, 04:31
von mit
Hallo,
Ich habe
I found a Django project and failed to get it running in Docker container in the following way:
  • `$ cat requirements.txt` in this files the below dependencies had to be updated:
    - psycopg2==2.8.6
Dies ist mein Dockerfile:

Code: Alles auswählen

FROM python:2
ENV PYTHONUNBUFFERED=1
RUN apt-get update && apt-get install -y postgresql-client
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/
RUN mkdir -p /var/log/django
RUN mkdir -p /var/log/i5k

Dies ist mein `docker-compose.yml`:

Code: Alles auswählen

version: "3"

services:
  db:
    image: postgres
    volumes:
      - ./data/db:/var/lib/postgresql/data
      - ./scripts/install-extensions.sql:/docker-entrypoint-initdb.d/install-extensions.sql

    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres

  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db
    links:
      - db

Code: Alles auswählen

$ cat scripts/install-extensions.sql 
CREATE EXTENSION hstore;

Diese Datei musste ich aendern:

Code: Alles auswählen

    $ vim i5k/settings_prod.py
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': 'postgres',
            'USER': 'postgres',
            'PASSWORD': 'postgres',
            'HOST': 'db',
            'PORT': '5432',
            }
    }

Code: Alles auswählen

$ docker-compose up --build
...
web_1  | Performing system checks...
web_1  | 
web_1  | System check identified no issues (0 silenced).
web_1  | Unhandled exception in thread started by <function wrapper at 0x7f8a9733a6d0>
web_1  | Traceback (most recent call last):
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 229, in wrapper
web_1  |     fn(*args, **kwargs)
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 116, in inner_run
web_1  |     self.check_migrations()
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 168, in check_migrations
web_1  |     executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 19, in __init__
web_1  |     self.loader = MigrationLoader(self.connection)
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/db/migrations/loader.py", line 47, in __init__
web_1  |     self.build_graph()
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/db/migrations/loader.py", line 191, in build_graph
web_1  |     self.applied_migrations = recorder.applied_migrations()
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 59, in applied_migrations
web_1  |     self.ensure_schema()
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 49, in ensure_schema
web_1  |     if self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()):
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 162, in cursor
web_1  |     cursor = self.make_debug_cursor(self._cursor())
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 135, in _cursor
web_1  |     self.ensure_connection()
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 130, in ensure_connection
web_1  |     self.connect()
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/db/utils.py", line 98, in __exit__
web_1  |     six.reraise(dj_exc_type, dj_exc_value, traceback)
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 130, in ensure_connection
web_1  |     self.connect()
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 119, in connect
web_1  |     self.connection = self.get_new_connection(conn_params)
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 176, in get_new_connection
web_1  |     connection = Database.connect(**conn_params)
web_1  |   File "/usr/local/lib/python2.7/site-packages/psycopg2/__init__.py", line 127, 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 "localhost" (127.0.0.1) and accepting
web_1  | 	TCP/IP connections on port 5432?
web_1  | could not connect to server: Cannot assign requested address
web_1  | 	Is the server running on host "localhost" (::1) and accepting
web_1  | 	TCP/IP connections on port 5432?
Was habe ich falsch gemacht?

Re: django.db.utils.OperationalError: could not connect to server: Connection refused

Verfasst: Sonntag 23. Mai 2021, 06:05
von sparrow
Du benutzt im Dockerfile Python 2. Python 2 ist tot. Es gibt weder neue Releases noch Sicherheitsupdates dafür.

Ansonsten ist die Fehlermeldung ja recht eindeutig: der Postgres Server ist nicht zu erreichen.
Wie sehr weißt du denn über Docker bescheid?

Re: django.db.utils.OperationalError: could not connect to server: Connection refused

Verfasst: Sonntag 23. Mai 2021, 09:58
von DasIch
Dein Beitrag gekürzt auf den Teil, der für die Frage in der Fehlermeldung relevant ist:
mit hat geschrieben: Sonntag 23. Mai 2021, 04:31

Code: Alles auswählen

web_1  | django.db.utils.OperationalError: could not connect to server: Connection refused
web_1  | 	Is the server running on host "localhost" (127.0.0.1) and accepting
web_1  | 	TCP/IP connections on port 5432?
web_1  | could not connect to server: Cannot assign requested address
web_1  | 	Is the server running on host "localhost" (::1) and accepting
web_1  | 	TCP/IP connections on port 5432?

Code: Alles auswählen

    depends_on:
      - db
    links:
      - db
Diese Datei musste ich aendern:

Code: Alles auswählen

    $ vim i5k/settings_prod.py
    DATABASES = {
            #...
            'HOST': 'db',
            'PORT': '5432',
            }
    }
Die Datenbank läuft nicht auf localhost:5432, sondern auf db:5432. Ich glaub du musstest die Datei nicht ändern, sondern eine andere.

Re: django.db.utils.OperationalError: could not connect to server: Connection refused

Verfasst: Sonntag 23. Mai 2021, 11:47
von mit
Ich habe dieses Projekt nicht programmiert aber ich möchte es gern benutzen. Ich verwende Docker selten. Postgres benutzt nicht das selbe Container als das Django Container. Deswegen kann ich localhost nicht verwenden.

Re: django.db.utils.OperationalError: could not connect to server: Connection refused

Verfasst: Sonntag 23. Mai 2021, 12:36
von __deets__
Container zu Container Kommunikation ist hier beschrieben: https://www.tutorialworks.com/container-networking/

Re: django.db.utils.OperationalError: could not connect to server: Connection refused

Verfasst: Montag 24. Mai 2021, 14:32
von mit
Die Loesung ist:

Code: Alles auswählen

$ vim i5k/settings.py
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'postgres',
        'USER': 'postgres',
        'PASSWORD': 'postgres',
        'HOST': 'db',
        'PORT': '5432',
        }
}