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

时间:2019-03-15 20:14:50

标签: python-3.x postgresql docker django-rest-framework

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

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

/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.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
我的依赖项:

  • Django == 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    

2 个答案:

答案 0 :(得分:0)

之所以发生此问题,是因为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?

您需要添加一些延迟机制,以使Django等待直到PostgreSQL容器实际准备好接收连接。有关更多详细信息,您可以查看my answer in here,它与MySQL有关,并且适用于PostgreSQL。

关于此问题,根据the following answer,可能也是出于相同的原因

/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.

因此,首先实现第一个链接中说明的wait-for-it,然后重试

答案 1 :(得分:0)

最后,我今天来到办公室,打开电脑,奇迹般地一切正常,我不知道为什么:(。感谢您的回答。