问题描述
与docker的新手,我试图通过pgAdmin容器的本地主机连接到postgres容器.
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
我成功连接了psql命令.
I succeed connecting with psql command.
psql -h localhost -U postgres -d postgres
但是当我在pgAdmin上使用与psql相同的参数创建服务器时,出现以下错误.
But when I create the server on pgAdmin with the same parameters as psql I got the following error.
无法连接到服务器:连接被拒绝服务器正在运行吗?在主机"localhost"(127.0.0.1)上并接受TCP/IP连接端口5432?无法连接到服务器:地址不可用服务器在主机"localhost"(:: 1)上运行并接受TCP/IP端口5432上是否有连接?
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?
我成功地连接了 docker inspect
在容器上提供的IP地址.
I succeed to connect throught the IPAddress given by docker inspect
on the container.
顺便说一句,我检查了postgresql.conf并断言 listen_addresses ='*'
以及pg_hba.conf包含 host所有所有md5
.
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 ?
推荐答案
在这种情况下:
- Pgadmin无法连接到本地主机,但psql可在docker外部运行. 都是pgadmin& amp;Postgres作为容器运行
- Pgadmin fails to connect to localhost, but psql works from outside docker.
- both pgadmin & Postgres are running as Containers
尽管您未指明是否这样做,但理想情况下,两个容器都可以作为自定义桥接网络以实现自动DNS解析.
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.
要查找在Docker运行时中创建的网络,请输入:$ docker network ls
To find out the networks created in your docker runtime, type:$ docker network ls
某些网络将在控制台中列出,也许您会发现一个 [name] _default
应该是您的网络.
Some networks will be listed in the console, maybe you'll find a [name]_default
it should be your network.
执行 docker network inspect [name] _default
它会显示很多信息,对我们来说最重要的是IPv4Address,如下所示:"7c3cd7532ab8aacc70830afb74adad7296d9c8ddd725c498af2d7ee2d2c2aadd":{名称":"intime_postegres_1","EndpointID":"56a9cb574469f22259497b72719f9f4a3e555b09f95058fcf389ef5287381f28","MacAddress":"02:42:ac:12:00:02","IPv4Address":"172.18.0.2/16","IPv6Address":"}
Executedocker 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": "" }
代替在pgAdmin新服务器对话框中使用localhost作为服务器名称/ip,而是连接到postgres实例的"IPv4Address".
Instead of using localhost for the server name/ip in the pgAdmin new server dialog, connect to the postgres instance's "IPv4Address".
在我的情况下,连接到 172.18.0.2:5432
的工作就像一种魅力.
In my case connecting at 172.18.0.2:5432
, worked like a charm.
这篇关于Docker-Postgres和pgAdmin 4:连接被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!