【问题标题】:Django was unable to create a connection to the 'postgres' database and will use the default database instead [closed]Django 无法创建到“postgres”数据库的连接,而是使用默认数据库 [关闭]
【发布时间】:2019-08-06 23:21:44
【问题描述】:

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

我在一个项目上工作,我尝试使用 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
我的依赖:
  • 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    

【问题讨论】:

  • 您是否尝试过仅使用链接而不是dependes_on?
  • 你能确认 postgres docker 容器正在运行吗?如果您使用docker psdocker-compose ps 的输出来编辑您的问题,那将会很有帮助。

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


【解决方案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?

您需要添加一些延迟机制,让 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,它在第一个链接中解释过,然后再试一次

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-12
    • 2011-08-31
    • 2019-12-14
    • 1970-01-01
    • 2014-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多