33

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 ?

Neok
  • 680
  • 1
  • 9
  • 22
  • 1
    Possible duplicate of [What does localhost means inside a Docker container?](https://stackoverflow.com/questions/50278632/what-does-localhost-means-inside-a-docker-container) – David Maze Dec 04 '18 at 12:06
  • I answered this here: https://stackoverflow.com/questions/25540711/docker-postgres-pgadmin-local-connection/57729412#57729412 – Afshin Ghazi Aug 30 '19 at 15:42

7 Answers7

66

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.

Alex
  • 963
  • 10
  • 11
29

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 - enter image description here

  • 1
    Create new server for Docker to access Postgres really helps me, thank you! – Tiara Jun 16 '22 at 01:03
  • Same, 172.17.0.2 worked for me. But it is troubling that localhost does not... why is this the case? Launching pgadmin in Docker, localhost in my browser brings up pgadmin... – Victorio Berra Dec 31 '22 at 20:49
12

I had the same issue and I solved it in a non-canonical way, but very satisfactory to my local workflow.

I always start my postgresql container with a docker-compose.yaml file. So I just added the pgAdmin to the same compose file:

version: "3.7"
services:
  my_awesome_db:
    image: postgres:latest
    ports:
      - "5432:5432"
    container_name: postgresql-local
    volumes:
      - "/var/run/postgres.sock:/var/run/postgres/postgres.sock"
      - "/home/myuser/docker-apps/volumes/postgres-data:/var/lib/postgresql/data"
  pg_admin:
    image: dpage/pgadmin4:latest
    container_name: pgadmin4
    ports:
      - "15432:80"
    environment:
      - GUNICORN_THREADS=1
      - PGADMIN_DEFAULT_EMAIL=my_awesome_email@email.com
      - PGADMIN_DEFAULT_PASSWORD=does_not_matter
    depends_on:
      - my_awesome_db

So I access pgAdmin on my localhost:15432 (just because it's easy to remember), and I've configured pgAdmin with:

Host: my_awesome_db
Port: 5432
Username: postgres
Rômulo Collopy
  • 944
  • 9
  • 13
  • I think "localhost:5050" is easy to remember but aside from that, your solution worked for me. I'm using Docker for Windows btw. Thanks! – Ekown Feb 14 '20 at 16:25
  • 2
    The key point here is to use container service name (`my_awesome_db`, `pg_admin`) as a host name when connecting through pgAdmin. From official docs [Networking in compose](https://docs.docker.com/compose/networking/): `By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.` – BeshEater Jul 21 '20 at 04:21
5

For me worked like this:

Add Server -> Connection -> Hostname/address

Set field to 172.17.0.1

More info at Docker Network doc: https://docs.docker.com/network/network-tutorial-standalone

jotafeldmann
  • 642
  • 10
  • 17
4

first you need to get container information

$docker ps
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

then extract IP Address

$sudo docker inspect e79fb6440a95 | grep IPAddress
            "SecondaryIPAddresses": null,
            "IPAddress": "",
                    "IPAddress": "192.168.64.2",

And when you create your server put your IP Address

enter image description here

alpha
  • 511
  • 8
  • 15
  • The `docker inspect` IP address isn't stable; if the container is deleted or recreated it's possible for that address to change. You shouldn't ever need to look it up. – David Maze Dec 20 '22 at 15:15
1

You just need to specify in your connection string the, server parameter, the name of your container name and not the localhost or 127.0.0.1

Stefan Gabor
  • 340
  • 3
  • 7
-1

Because I was connecting to a server on the hose, it was as simple as adding

--net host

to the docker run command, so that the container gets access to the host's ports.

QA Collective
  • 2,222
  • 21
  • 34