Python 3.x Django无法创建到';博士后';数据库,并将使用默认数据库
这个社区是我解决这个问题的最后手段,因为我已经为此奋斗了几个小时:( 我在一个项目中工作,尝试使用docker compose进行测试,使用这个命令Python 3.x Django无法创建到';博士后';数据库,并将使用默认数据库,python-3.x,postgresql,docker,django-rest-framework,Python 3.x,Postgresql,Docker,Django Rest Framework,这个社区是我解决这个问题的最后手段,因为我已经为此奋斗了几个小时:( 我在一个项目中工作,尝试使用docker compose进行测试,使用这个命令docker compose运行runserver python3.6 manage.py test,但是,我不明白它来自何处以及如何解决这个问题。以下是我的错误 /usr/local/lib/python3.6/dist packages/django/db/backends/postgresql/base.py:267:RuntimeWarnin
docker compose运行runserver python3.6 manage.py test
,但是,我不明白它来自何处以及如何解决这个问题。以下是我的错误
/usr/local/lib/python3.6/dist packages/django/db/backends/postgresql/base.py:267:RuntimeWarning:通常,django将使用到“postgres”数据库的连接,以避免在不需要时(例如,在运行测试时)对生产数据库运行初始化查询.Django无法创建到“postgres”数据库的连接,将使用默认数据库。
及
psycopg2.OperationalError:无法连接到服务器:连接超时服务器是否在主机“postgres”(172.18.0.3)上运行,并在端口5432上接受TCP/IP连接?
因此,码头工人工作得很好
这是docker-compose.yml:
版本:“3”
服务:
博士后:
图片:postgres:最新
环境:
POSTGRES_DB:${POSTGRES_DB}
POSTGRES_用户:${POSTGRES_用户}
POSTGRES_密码:${POSTGRES_密码}
卷数:
-路径卷
端口:
- "5432:5432"
弹性搜索:
图像:路径搜索
端口:
- "9200:9200"
环境:
-xpack.security.enabled=false
乌利米特:
nofile:
电话:65536
硬盘:65536
卷数:
-路径卷
rabbitmq:
图片:rabbitmq:3
端口:
- "5672:5672"
- "15672:15672"
redis:
图片:redis
芹菜:
建造:
上下文:。
环境文件:.env
卷数:
-.:/opt/project
取决于:
-博士后
-弹性搜索
-兔子
-雷迪斯
命令:芹菜-项目工作者-beat-l调试
运行服务器:
建造:
上下文:。
环境文件:.env
卷数:
-.:/opt/project
-/opt/project/src
取决于:
-博士后
-弹性搜索
-兔子
-雷迪斯
-芹菜
命令:python3.6 manage.py runserver 0.0.0.0:8000
端口:
-8000:8000
发生此问题是因为Django希望数据库能够启动并运行(读取连接)
您需要添加一些延迟机制,使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.
因此,首先执行第一个链接中解释的
等待它
,然后再试一次您是否尝试使用链接而不是仅依赖于?您能否确认postgres docker容器正在运行?如果您使用docker ps
或docker compose ps
的输出编辑您的问题,那将是他一杯。
/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.