java - 连接到 127.0.0.1 :5432 refused. 检查主机名和端口是否正确以及邮局管理员是否接受 TCP/IP 连接

标签 java postgresql docker ubuntu

我正在从 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/

相关文章:

java - 在 jruby 中实现自定义 java 类

java - Java 中的约瑟夫斯游戏

sql - 删除空行,其中列是动态选择的

docker - kubernetes设置所有物理节点的列表

java - 没有额外的 readLine() 循环就无法检测到断开连接

java - 数独生成器的递归求解

sql - 如何在 SQL 中按月按产品创建数据透视表

postgresql - 如何将CSV数据插入PostgreSQL数据库(远程数据库)

docker - 如何向 OpenShift/Kubernetes 部署提供环境信息(如外部服务 URL 或密码)?

docker - 如何在Docker中公开从主机到容器的端口?