41

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 ?

2

11 Answers 11

76

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.

If on the other hand your postgresql container was created with a docker-compose.yml and already had a custom network defined, you need to start (run) your dpage/pgadmin4 container with the --network arg giving it the name of your already running custom Docker network. Let's say your already running custom Docker network is called my-custom-network. Your pgadmin4 Docker startup command should be:

$ docker run -p 7080:80 --network my-custom-network -d dpage/pgadmin4

Once you start your pgadmin4 container this way, it now has access to the postgres container defined earlier in your my-custom-network. So you can either refer to it by its IPv4 address (as explained above), or by its DNS name internal to your custom network.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks it works but isn't foolproof as a restart would change the ip.
Yes, it´s true I haven't figure it out, but maybe has a good solution for that. I only used on dev environment. Do u have any clue?
Using the name instead of the ip works even when you restart.
The solution work for locally or same domain/intranet only. It doesn't work for public ips
40

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

3 Comments

Create new server for Docker to access Postgres really helps me, thank you!
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...
In my case I had to use the Gateway ip address
19

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
      - [email protected]
      - 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

2 Comments

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!
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: 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.
8

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

Comments

6

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

1 Comment

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.
2

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

1 Comment

Can you please share a screenshot, or code snippet?
0

My pgAdmin runs on the docker host (osx). In docker-compose.yml I had to set

ports:
  - 5432:5432

explicit. This line was not there before. But this oneliner did the trick.

Here's my example:

version: '3.9'

services:

  db:
    image: postgres
    restart: always
    environment:
      POSTGRES_PASSWORD: mysecret
      POSTGRES_USER: myuser
      POSTGRES_DB: mydb
    ports:
      - 5432:5432
    ...
    ...

After that, I can connect to "localhost" with pgadmin. And it is save if you restart your container! You don't need to search for the ipaddress with docker inspect

If you don't do this, your docker will not have a HostIp "0.0.0.0" but only a tcp 5432 connection.

Background information:

  1. Check your running docker container docker ps
  2. Get the name of your pgsql and do docker inspect name_of_container_here
  3. Find the row "Ports:

Before change the docker-compose.yml the docker-inspect ... will show the "Ports" 5432/tcp is null:

...
"Ports": {
    "5432/tcp": null
},
...

After changing the docker-compose.yml, docker inspect ... will show you the following, where you now can see "HostIp": "0.0.0.0", aso ...

...
"Ports": {
    "5432/tcp": [
        {
           "HostIp": "0.0.0.0",
           "HostPort": "5432"
        }
    ]
},
...

Comments

0

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

--net host

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

Comments

0

For me bringing up the containers via docker-compose did the trick, before I tried to do it by starting 2 postgres and pgadmin separately and things didn't work. Will leave here the docker-compose.yml that worked for me:

version: '3.8'

services:
  postgres:
    image: postgres
    container_name: postgres
    environment:
      POSTGRES_PASSWORD: postgres
    ports:
      - "5432:5432"
    volumes:
      - pgdata:/var/lib/postgresql/data

  pgadmin:
    image: dpage/pgadmin4
    container_name: pgadmin
    environment:
      PGADMIN_DEFAULT_EMAIL: [email protected]
      PGADMIN_DEFAULT_PASSWORD: password
    ports:
      - "82:80"
    depends_on:
      - postgres

volumes:
  pgdata:

Comments

0

In my case, I am connecting in pgadmin 4 by setting Host as localhost!

PgAdmin connection properties

docker-compose.yml:

timesheets.db:
  image: postgres:latest
  container_name: timesheets.db
  environment: 
    - POSTGRES_DB=timesheets
    - POSTGRES_USER=postgres
    - POSTGRES_PASSWORD=postgres
  volumes:
    - ./.containers/timesheets-db:/var/lib/postgresql/data
  hostname: timesheets.db
  ports:
    - 5432:5432

Comments

-1

it worked for me too, I used localhost as hostname

2 Comments

This response is more appropriate as a comment rather than an answer.
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.