我在Django和Docker上使用Postgres,但在重新初始化后无法连接到数据库。

huangapple go评论59阅读模式
英文:

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 -vsudo 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 -vand 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.

huangapple
  • 本文由 发表于 2023年7月12日 23:18:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76672140.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定