Docker - Postgres and pgAdmin 4 : Connection refused
In this case:
- Pgadmin fails to connect to localhost, but psql works from outside docker.
- both pgadmin & Postgres are running as Containers
Although you haven't indicated if you are doing so, ideally both containers could be part of a custom bridge network for automatic DNS resolution.
If not added explicitly they will be part of the default bridge network.
To find out the networks created in your docker runtime, type:
$ docker network ls
Some networks will be listed in the console, maybe you'll find a [name]_default
it should be your network.
Execute
docker network inspect [name]_default
it'll show up a bunch of information, for us the most important is IPv4Address, something like this:
"7c3cd7532ab8aacc70830afb74adad7296d9c8ddd725c498af2d7ee2d2c2aadd": {
"Name": "intime_postegres_1",
"EndpointID": "56a9cb574469f22259497b72719f9f4a3e555b09f95058fcf389ef5287381f28",
"MacAddress": "02:42:ac:12:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
}
Instead of using localhost for the server name/ip in the pgAdmin new server dialog, connect to the postgres instance's "IPv4Address".
In my case connecting at 172.18.0.2:5432
, worked like a charm.
I too had the case when you're able to connect with psql
command on terminal but not with pgAdmin4.
The following solution worked for me.
First -
docker ps
From there you'll get the container name/ID of the postgres container, then do -
docker inspect name_of_container_here
It'll give you something like this -
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "f35dbe66b36cd38673aad6e1278ad33031ef9d5abd34582d4b91955e288f855e",
"EndpointID": "2e63ea59e9d0de7526bb02ba29bc0b16bcad51b8963127ad380416d15da994db",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
Note the IPAddress and provide that while creating a new server in pgAdmin4 -