Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?

  • 4,000
  • Tác giả: admin
  • Ngày đăng:
  • Lượt xem: 4
  • Tình trạng: Còn hàng

I encountered a similar problem when I was trying vĩ đại connect my Django application vĩ đại PostgreSQL database.

I wrote my Dockerfile with instructions vĩ đại setup the Django project followed by instructions vĩ đại install PostgreSQL and run rẩy Django server in my docker-compose.yml.

I defined two services in my docker-compose-yml.

services:
  postgres:
    image: "postgres:latest"
    environment:
      - POSTGRES_DB=abc
      - POSTGRES_USER=abc
      - POSTGRES_PASSWORD=abc
    volumes:
      - pg_data:/var/lib/postgresql/data/
  django:
    build: .
    command: python /code/manage.py runserver 0.0.0.0:8004
    volumes:
      - .:/app
    ports:
      - 8004:8004
    depends_on:
      - postgres 

Unfortunately whenever I used vĩ đại run rẩy docker-compose up then same err. used vĩ đại pop up.

And this is how my database was defined in Django settings.py.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'abc',
        'USER': 'abc',
        'PASSWORD': 'abc',
        'HOST': '127.0.0.1',
        'PORT': '5432',
        'OPTIONS': {
            'client_encoding': 'UTF8',
        },
    }
} 

So, In the kết thúc I made use of docker-compose networking which means if I change the host of my database vĩ đại postgres which is defined as a service in docker-compose.yml will tự the wonders.

So, Replacing 'HOST': '127.0.0.1' => 'HOST': 'postgres' did wonders for bủ.

After replacement this is how your Database config in settings.py will look lượt thích.

DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql',
            'NAME': 'abc',
            'USER': 'abc',
            'PASSWORD': 'abc',
            'HOST': 'postgres',
            'PORT': '5432',
            'OPTIONS': {
                'client_encoding': 'UTF8',
            },
        }
    }