我正在从 docker-compose 运行我的代码,它试图访问本地数据库(postgres)。 每当我尝试运行时,我都会收到此错误
Connection to 127.0.0.1:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
但是 postgres 正在端口 5432
上运行
这是 sudo netstat -ltnp | grep postgres
的结果
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 1995/postgres
我在 ubunto 操作系统上使用 postgres v 10.9。
最佳答案
If you try to access to localhost inside a container then it's the localhost of the container not your machine or any other container. If both your app and database are declare in the same compose, use the same network you can use the container name
这是正确的,但如果您在本地计算机上安装了 Postgres,则可以绕过设置计算机 IP 的容器限制,例如连接到 Postgres 到:5432
如果仍然无法正常工作,请检查本地计算机的端口 5432 上是否有任何防火墙限制,并检查 Postgres 是否设置为从外部源接收数据。
另外,这个示例应该只在“家里”使用,我不建议在公司网络或任何生产环境中使用它。
关于java - 连接到 127.0.0.1 :5432 refused. 检查主机名和端口是否正确以及邮局管理员是否接受 TCP/IP 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61404096/