Django 无法创建与“postgres”数据库的连接,将使用默认数据库

wil*_*oua 5 postgresql python-3.x django-rest-framework docker

这个社区是我解决这个问题的最后手段,因为我已经为此奋斗了几个小时:(。

我在一个项目上工作,我尝试使用 docker-compose 进行测试,使用这个命令,docker-compose run runserver python3.6 manage.py test但是,我不明白它来自哪里以及如何解决这个问题。这是我的错误

/usr/local/lib/python3.6/dist-packages/django/db/backends/postgresql/base.py:267: RuntimeWarning: Normally Django will use a connection to the 'postgres' database to avoid running initialization queries against the production database when it's not needed (for example, when running tests). Django was unable to create a connection to the 'postgres' database and will use the default database instead.

psycopg2.OperationalError: could not connect to server: Connection timed out Is the server running on host "postgres" (172.18.0.3) and accepting TCP/IP connections on port 5432?

所以,docker-compose up 工作得很好。

这是 docker-compose.yml:

version: '3'

services:
  postgres:
    image: postgres:latest
    environment:
      POSTGRES_DB: ${POSTGRES_DB}
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    volumes:
      - path_volume
    ports:
      - "5432:5432"

  elasticsearch:
    image: path_elastic_search
    ports:
      - "9200:9200"
    environment:
      - xpack.security.enabled=false
    ulimits:
      nofile:
        soft: 65536
        hard: 65536
    volumes:
      - path_volume

  rabbitmq:
    image: rabbitmq:3
    ports:
      - "5672:5672"
      - "15672:15672"

  redis:
    image: redis

  celery:
    build:
      context: .
    env_file: .env
    volumes:
      - .:/opt/project
    depends_on:
      - postgres
      - elasticsearch
      - rabbitmq
      - redis
    command: celery -A project worker --beat -l debug

  runserver:
    build:
      context: .
    env_file: .env
    volumes:
      - .:/opt/project
      - /opt/project/src
    depends_on:
      - postgres
      - elasticsearch
      - rabbitmq
      - redis
      - celery
    command: python3.6 manage.py runserver 0.0.0.0:8000
    ports:
      - 8000:8000
Run Code Online (Sandbox Code Playgroud)
我的依赖项:

  • 姜戈==1.11.5
  • postgres==9

命令行的响应docker-compose ps是:

            Name                          Command                State                                          Ports                                     
----------------------------------------------------------------------------------------------------------------------------------------------------------
datablitzapi_celery_1          celery -A datablitz worker ...   Up                                                                                        
datablitzapi_elasticsearch_1   /bin/bash bin/es-docker          Up         0.0.0.0:9200->9200/tcp, 9300/tcp                                               
datablitzapi_postgres_1        docker-entrypoint.sh postgres    Up         0.0.0.0:5432->5432/tcp                                                         
datablitzapi_rabbitmq_1        docker-entrypoint.sh rabbi ...   Up         0.0.0.0:15672->15672/tcp, 25672/tcp, 4369/tcp, 5671/tcp, 0.0.0.0:5672->5672/tcp
datablitzapi_redis_1           docker-entrypoint.sh redis ...   Up         6379/tcp                                                                       
datablitzapi_runserver_1       python3.6 manage.py runser ...   Exit 255   0.0.0.0:8000->8000/tcp    
Run Code Online (Sandbox Code Playgroud)

Mos*_*ein 1

发生此问题是因为 Django 期望数据库将启动并运行(读取连接)

psycopg2.OperationalError: could not connect to server: Connection timed out Is the server running on host "postgres" (172.18.0.3) and accepting TCP/IP connections on port 5432?
Run Code Online (Sandbox Code Playgroud)

您需要的是添加一些延迟机制,使 Django 等待,直到您的 PostgreSQL 容器真正准备好接收连接。有关更多详细信息,您可以查看我的回答,这是关于 MySQL 的,同样的情况也适用于 PostgreSQL。

关于这个问题,根据下面的回答,可能也是因为同样的原因

/usr/local/lib/python3.6/dist-packages/django/db/backends/postgresql/base.py:267: RuntimeWarning: Normally Django will use a connection to the 'postgres' database to avoid running initialization queries against the production database when it's not needed (for example, when running tests). Django was unable to create a connection to the 'postgres' database and will use the default database instead.
Run Code Online (Sandbox Code Playgroud)

因此,首先实施wait-for-it第一个链接中解释的内容,然后重试