Django 无法创建到“postgres”数据库的连接,而是使用默认数据库 [关闭]
Posted
技术标签:
【中文标题】Django 无法创建到“postgres”数据库的连接,而是使用默认数据库 [关闭]【英文标题】:Django was unable to create a connection to the 'postgres' database and will use the default database instead [closed] 【发布时间】: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 ps
或docker-compose ps
的输出来编辑您的问题,那将会很有帮助。
【参考方案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
,它在第一个链接中解释过,然后再试一次
【讨论】:
以上是关于Django 无法创建到“postgres”数据库的连接,而是使用默认数据库 [关闭]的主要内容,如果未能解决你的问题,请参考以下文章
使用 postgres 在 AWS 上部署的 django 项目 - 如何创建数据库
Django - 无法使用两个数据库进行测试(带有 gis 扩展的 postgres 和 mongoDB (Djongo)
无法让 Django 在 k8s 中与 Postgres 对话