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:
      - PGADMIN_DEFAULT_EMAIL=admin@mydomain.com
      - 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      -                   
$
pgkt
  • 61
  • 1
  • 6

2 Answers2

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!

Basil Eric Rabi
  • 313
  • 1
  • 7
  • second option worked for me, first one (which I find more elegant) didn't for some reason – Rumpelstiltskin Koriat May 20 '21 at 20:37
  • 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! – Jeach May 28 '23 at 07:00
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?

Anubisoft
  • 91
  • 2