[英]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
I'm running my code from docker-compose which is trying to access a local database(postgres).我正在尝试访问本地数据库(postgres)的 docker-compose 运行我的代码。 whenever i try to run i get this error每当我尝试运行时,我都会收到此错误
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.
but postgres is running on port 5432
但是 postgres 在端口5432
上运行
here is ther result of sudo netstat -ltnp | grep postgres
这是sudo netstat -ltnp | grep postgres
的结果sudo netstat -ltnp | grep postgres
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 1995/postgres
I'am using postgres v 10.9 on ubunto OS.我在 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如果您的应用程序和数据库都在同一个组合中声明,请使用相同的网络,您可以使用容器名称
That is correct, but if you have Postgres installed in you local machine, you could bypass the container restriction setting your machine's IP, eg connecting to Postgres to:5432这是正确的,但如果你在本地机器上安装了 Postgres,你可以绕过容器限制设置你机器的 IP,例如连接到 Postgres 到:5432
If still not working, check if you have any firewall restriction on your local machine, on port 5432 and check if Postgres is set to recive data from external source.如果仍然无法正常工作,请检查本地计算机上的 5432 端口是否有任何防火墙限制,并检查 Postgres 是否设置为从外部源接收数据。
Plus this example should be only used "at home", I do not suggest to use it in company network or any production environment.加上这个例子应该只在“家里”使用,我不建议在公司网络或任何生产环境中使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.