Docker - Postgres and pgAdmin 4 : Connection refused

和自甴很熟 提交于 2021-02-04 13:16:28

问题


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 ?


回答1:


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.




回答2:


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 -




回答3:


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



回答4:


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




回答5:


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.



来源:https://stackoverflow.com/questions/53610385/docker-postgres-and-pgadmin-4-connection-refused

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!