[英]Cannot connect to PostgreSQL in Docker through PHP
I'm cant connect to PostgreSQL with php code. 我无法用PHP代码连接到PostgreSQL。 My Postgresql is docker container.
我的Postgresql是docker容器。 I get an IP with:
我得到一个IP:
docker inspect toshi_db_1
How I'm trying to connect: 我是如何尝试连接的:
$dbconn = pg_connect("host=172.17.0.2 port=5432 dbname=toshi_development")or die("Could not connect");
Error : Warning: pg_connect(): Unable to connect to PostgreSQL server: could not connect to server: Connection timed out (0x0000274C/10060) Is the server running on host "172.17.0.2" and accepting TCP/IP connections on port 5432?
错误 :警告:pg_connect():无法连接到PostgreSQL服务器:无法连接到服务器:连接超时(0x0000274C / 10060)服务器是否在主机“172.17.0.2”上运行并接受端口5432上的TCP / IP连接?
I thought there could be problems in PostgreSQL.conf with listen_address configuration parameter, but it allows all connections, so I have no idea where the problem is. 我认为PostgreSQL.conf中可能存在listen_address配置参数的问题,但它允许所有连接,所以我不知道问题出在哪里。
UPDATE : I fixed it myself, just tried to expose wrong port in docker-compose file.
更新 :我自己修复了它,只是试图在docker-compose文件中暴露错误的端口。
I run in the same situation today. 我今天也遇到了同样的情况。 The solution was replacing the localhost port with the name of the container.
解决方案是使用容器的名称替换localhost端口。 Like:
喜欢:
$dbconn = pg_connect("host=toshi_db_1 port=5432 dbname=toshi_development")or die("Could not connect");
Not sure if it should be the same with IPs. 不确定它是否应与IP相同。 But doesn't the IPs change?
但IP不会改变吗?
So, for other people seeing this post: remember that 127.0.0.1 always points to current container. 所以,对于其他人看到这篇文章:记住127.0.0.1始终指向当前容器。 So you must replace that "localhost" part with your container name.
因此,您必须用容器名称替换“localhost”部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.