gpt4 book ai didi

postgresql - Dockerizing PostgreSQL - psql 连接被拒绝

转载 作者:IT老高 更新时间:2023-10-28 12:47:33 24 4
gpt4 key购买 nike

我正在使用 Docker,我想 Dockerize 一个 Postgres 容器。

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

我使用示例的内容创建了 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

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

~/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

所以我可以使用 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?

感谢任何帮助。

最佳答案

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

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

关于postgresql - Dockerizing PostgreSQL - psql 连接被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26343178/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com