Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker - Postgres and pgAdmin 4 : Connection refused

Tags:

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 ?

like image 259
Neok Avatar asked Dec 04 '18 10:12

Neok


People also ask

Why is PgAdmin not connecting to server?

If pgAdmin displays this message, there are two possible reasons for this: the database server isn't running - simply start it. the server isn't configured to accept TCP/IP requests on the address shown.

How does Docker integrate with PostgreSQL?

Fill the port value as 5432 that runs the Docker PostgreSQL Container and provide the name of the database as postgres. Then, fill the username and password fields with the credentials you created while running the PGAdmin container. After providing all required details, click on the “Save” button.


1 Answers

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.

like image 124
Alex Avatar answered Sep 19 '22 17:09

Alex