Dockerizing PostgreSQL - psql 连接被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26343178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:47:51  来源:igfitidea点击:

Dockerizing PostgreSQL - psql Connection refused

postgresqldockerpsql

提问by Kummo

I'm playing around with Docker and I would like to Dockerize a Postgres container.

我在玩 Docker,我想 Dockerize 一个 Postgres 容器。

I'm following the official examplebut I can not connect to the image running using psql.

我正在关注官方示例,但无法连接到使用 psql 运行的图像。

I created the Dockerfile with the content of the example. I builded an image from the Dockerfile and assigned it a name. Then I run the PostgreSQL server container (in the foreground).

我使用示例的内容创建了 Dockerfile。我从 Dockerfile 构建了一个映像并为其分配了一个名称。然后我运行 PostgreSQL 服务器容器(在前台)。

~/test ? docker run --rm -P --name pg_test eg_postgresql                                                                                                       
2014-10-10 06:12:43 UTC LOG:  database system was interrupted; last known up at 2014-10-10 06:12:29 UTC
2014-10-10 06:12:43 UTC LOG:  database system was not properly shut down; automatic recovery in progress
2014-10-10 06:12:43 UTC LOG:  redo starts at 0/1782F68
2014-10-10 06:12:43 UTC LOG:  record with zero length at 0/1782FA8
2014-10-10 06:12:43 UTC LOG:  redo done at 0/1782F68
2014-10-10 06:12:43 UTC LOG:  last completed transaction was at log time 2014-10-10 06:12:29.2487+00
2014-10-10 06:12:43 UTC LOG:  database system is ready to accept connections
2014-10-10 06:12:43 UTC LOG:  autovacuum launcher started

Then I open another terminal to find out the port:

然后我打开另一个终端来找出端口:

~/test ? docker ps                                                                                                                                             
CONTAINER ID        IMAGE                  COMMAND                CREATED             STATUS              PORTS                     NAMES
aaedb0479139        eg_postgresql:latest   "/usr/lib/postgresql   3 days ago          Up 41 seconds       0.0.0.0:49154->5432/tcp   pg_test

So I can use psql to connect to the instance. But I can't...

所以我可以使用 psql 连接到实例。但我不能...

~/test ? psql -h localhost -p 49154 -d docker -U docker --password                                                                                             
Password for user docker:
psql: 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 49154?
could not connect to server: Connection refused
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 49154?
could not connect to server: Connection refused
    Is the server running on host "localhost" (fe80::1) and accepting
    TCP/IP connections on port 49154?

Any help is appreciated.

任何帮助表示赞赏。

采纳答案by wf.

Running this on my mac worked for me:

在我的 mac 上运行这个对我有用:

 $ boot2docker ip
 The VM's Host only interface IP address is: 192.168.59.103

And then connect along the lines of:

然后按照以下方式连接:

 $ psql -h 192.168.59.103 -p 49159 -d docker -U docker --password

It's less than ideal to have to do this all, but the instructions at https://docs.docker.com/installation/mac/indicate it is the correct solution if you want to connect directly from you mac.

必须完成这一切并不理想,但https://docs.docker.com/installation/mac/ 上的说明表明,如果您想直接从 mac 连接,这是正确的解决方案。

回答by Christopher Rapcewicz

If you add the --publish option to the docker run command

如果将 --publish 选项添加到 docker run 命令

docker run --rm -P --publish 127.0.0.1:5432:5432 --name pg_test eg_postgresql 

when you run the docker file, then the following will work (note the port is now 5432)

当您运行 docker 文件时,以下将起作用(注意端口现在是 5432)

psql -h localhost -p 5432 -d docker -U docker --password

回答by Luiz Fernando Corrêa Leite

Solution for me was simply using host.docker.internalinstead of localhostin your connection string.

对我来说,解决方案只是在您的连接字符串中使用host.docker.internal而不是使用localhost

https://github.com/npgsql/Npgsql.EntityFrameworkCore.PostgreSQL/issues/225

https://github.com/npgsql/Npgsql.EntityFrameworkCore.PostgreSQL/issues/225