英文:
I'm using Django and Postgres on Docker, but I can't connect to the database after reinitialising
问题
重要提示:Django 无法重新连接到数据库。它在运行时正常工作,然后我尝试重新初始化它,由于下面描述的原因它停止工作。不过,如果我重新启动我的电脑,它就可以正常工作。
我正在使用Django和Docker Compose运行PostgreSQL(一个用于Django,另一个用于PostgreSQL)容器。我一直都能够完美地使用这个容器,没有任何问题。但在输入sudo docker compose down -v
和sudo docker compose up -d
之后,Firefox 无法连接,并显示以下消息:
连接被重置
所以我运行了sudo docker compose logs -f
来查看日志,Django 表示无法连接到Postgres数据库,并显示以下消息:
[...] 端口 5432 连接失败:连接被拒绝
服务器是否在该主机上运行并接受 TCP/IP 连接?
英文:
IMPORTANT NOTE: Django can't RECONNECT to the database. It works, then I try reinitialising it, and it stops working due to what's described below. I can get it to work if I reboot my PC, though.
I'm running Django and PostgreSQL with Docker Compose (one container for Django and the other for PostgreSQL) I was using the container perfectly with no problems. After typing sudo docker compose down -v
and sudo docker compose up -d
, Firefox won't connect, and will show the message:
>The connection was reset
So I run sudo docker compose logs -f
to check the logs and Django says it can't connect to the Postgres database with the following messages:
>[...]port 5432 failed: Connection refused
>Is the server running on that host and accepting TCP/IP connections?
答案1
得分: 0
警告:在尝试此操作之前,请备份您的数据(容器内部)。
重新构建一切都成功了。我只需要输入 sudo docker compose up -d --build
,而且不需要重新启动我的计算机。虽然这个方法有效,但我不能说这是最终的答案,因为数据库会丢失所有数据,但由于我的应用程序仍处于开发阶段,这目前不是问题。
英文:
WARNING: back up your data (inside the containers) before trying this.
Rebuilding everything worked. All I had to do was type sudo docker compose up -d --build
and I didn't need to reboot my computer for this. While this works, I wouldn't say it's the definitive answer because the database loses all its data, but that isn't a problem right now since my app is still in the development stage.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论