Can't connect to postgres db from pgadmin (both running on docker)?
Asked Answered
T

3

6

I'm running postgres and pgadmin4 on docker with docker-compose up on a fedora 28 OS and I'm having trouble creating a new db server from pgadmin's web console. This is the docker-compose.yml file I'm using.

version: '3.0'

services:
  db:
    image: postgres:9.6
    ports:
      - 5432:5432/tcp
    environment:
      - POSTGRES_USER=admin
      - POSTGRES_PASSWORD=admin
      - POSTGRES_DB=mydb

  pgadmin:
    image: dpage/pgadmin4
    ports:
      - 5454:5454/tcp
    environment:
      - [email protected]
      - PGADMIN_DEFAULT_PASSWORD=postgres
      - PGADMIN_LISTEN_PORT=5454

What should I write in the Create new server > Connection tab > "Host name/address" field? If I type in localhost or 127.0.0.1 I get an error (Unable to connect, see screenshot1 and screenshot2). If I type db (the service's name as specified in the yml file), only then pgadmin accepts it and creates a db server with a postgres database called mydb.

Why? How do I find the ip that goes in the address field?

Furthermore, on Fedora28:

$ netstat -napt | grep LIST
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      -                   
tcp        0      0 127.0.0.1:3350          0.0.0.0:*               LISTEN      -                   
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:3389            0.0.0.0:*               LISTEN      -                   
tcp6       0      0 :::5454                 :::*                    LISTEN      -                   
tcp6       0      0 ::1:631                 :::*                    LISTEN      -                   
tcp6       0      0 :::5432                 :::*                    LISTEN      -                   
$
Timm answered 20/3, 2019 at 16:8 Comment(0)
C
5

I encountered this problem just recently too. There are two approaches I found:

1) See here. Basically, you just search for the IP address of the postgres container and use that IP address in pgadmin4:

$ docker ps
CONTAINER ID        IMAGE                      COMMAND                  CREATED             STATUS                    PORTS                           NAMES
194d4a5f9dd0        dpage/pgadmin4             "/entrypoint.sh"         48 minutes ago      Up 48 minutes             443/tcp, 0.0.0.0:8080->80/tcp   docker-postgis_pgadmin_1
334d5bdc87f7        kartoza/postgis:11.0-2.5   "/bin/sh -c /docke..."   48 minutes ago      Up 48 minutes (healthy)   0.0.0.0:5432->5432/tcp          docker-postgis_db_1

In my case, the postgres container ID is 334d5bdc87f7. Then look for the IP address:

$ docker inspect 334d5bdc87f7 | grep IPAddress
            "SecondaryIPAddresses": null,
            "IPAddress": "",
                    "IPAddress": "172.18.0.2",

When I used 172.18.0.2 in pgadmin4, I connected to the database! Yey!

2) The 2nd approach is easier. Instead of using localhost or 127.0.0.1 or ::1, I used my IP address in my local network (e.g. in your case 192.168.122.1?). Afterwards, I connected to the postgres container!

Cantonment answered 11/4, 2019 at 12:49 Comment(2)
second option worked for me, first one (which I find more elegant) didn't for some reasonHerculie
I had the same issue two years ago and found your answer, which worked. Now, two years later, I had completely forgotten about this detail. Trying to use localhost wasn't working. The moment I saw your answer, I realised I had forgotten about this. Works great, thanks!Torritorricelli
L
0

From my reading of the docs and testing this myself you're doing it right using the database service name from the docker-compose yml file as the "Host name/address" field value in pgAdmin.

https://docs.docker.com/compose/networking/

In your "pgadmin" section I would use port values of 8080 or 80, why 5454?

Luminous answered 7/4, 2019 at 20:1 Comment(0)
R
0

If you're using docker compose to manage your docker containers you can simply connect via the convenient image name specified in the docker-compose.yaml-file.

For instance, I have two named containers running called postgres and pgadmin with the following setup

services:
  postgres:
    env_file: .env
    image: postgres:latest
    container_name: postgres
    ports:
      - "5432:5432"
    volumes:
      - db:/var/lib/postgresql/data

  pgadmin:
    image: dpage/pgadmin4
    container_name: pgadmin
    ports:
      - "5050:80"
    env_file: .env

volumes:
  db:


Since the postgres process is named postgres and is running inside a docker container, on the same network as the pgadmin-process, I can simply refer to it as postgres in the connection settings in the pgadmin gui.

Like so: adding postgres server in a docker network

Rieth answered 24/2, 2024 at 14:2 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.