postgresqldocker

Docker - Postgres and pgAdmin 4 : Connection refused


Newbie with docker, I am trying to connect throught localhost my pgAdmin container to the postgres one.

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                         NAMES
0b00555238ba        dpage/pgadmin4      "/entrypoint.sh"         43 minutes ago      Up 43 minutes       0.0.0.0:80->80/tcp, 443/tcp   pedantic_turing
e79fb6440a95        postgres            "docker-entrypoint.s…"   About an hour ago   Up About an hour    0.0.0.0:5432->5432/tcp        pg-docker

I succeed connecting with psql command.

psql -h localhost -U postgres -d postgres

But when I create the server on pgAdmin with the same parameters as psql I got the following error.

Unable to connect to server:

could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432? could not connect to server: Address not available Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?

I succeed to connect throught the IPAddress given by docker inspect on the container.

By the way, I checked postgresql.conf and assert that listen_addresses = '*' and also that pg_hba.conf contain host all all all md5.

But I don't get it, why shouldn't I be able to use the localhost address ? And why does docker even give me an address that is not local ?


Solution

  • In this case:

    1. Pgadmin fails to connect to localhost, but psql works from outside docker.
    2. 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.

    If on the other hand your postgresql container was created with a docker-compose.yml and already had a custom network defined, you need to start (run) your dpage/pgadmin4 container with the --network arg giving it the name of your already running custom Docker network. Let's say your already running custom Docker network is called my-custom-network. Your pgadmin4 Docker startup command should be:

    $ docker run -p 7080:80 --network my-custom-network -d dpage/pgadmin4
    

    Once you start your pgadmin4 container this way, it now has access to the postgres container defined earlier in your my-custom-network. So you can either refer to it by its IPv4 address (as explained above), or by its DNS name internal to your custom network.