I'm cant connect to PostgreSQL with php code. My Postgresql is docker container. I get an IP with:
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?
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.
UPDATE: I fixed it myself, just tried to expose wrong port in docker-compose file.
I run in the same situation today. The solution was replacing the localhost port with the name of the container. 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. But doesn't the IPs change?
So, for other people seeing this post: remember that 127.0.0.1 always points to current container. So you must replace that "localhost" part with your container name.
User contributions licensed under CC BY-SA 3.0