Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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      -                   
$
like image 523
pgkt Avatar asked Mar 20 '19 16:03

pgkt


1 Answers

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!

like image 118
Basil Eric Rabi Avatar answered Sep 23 '22 19:09

Basil Eric Rabi