gpt4 book ai didi

postgresql - 连接到通过ubuntu服务器上的docker运行的postgres实例

转载 作者:行者123 更新时间:2023-12-02 19:23:32 26 4
gpt4 key购买 nike

通过 docker 运行Postgresql实例时如何连接它。
在我的docker-compose中,我将图像定义如下:

db:
image: postgres:9.4
#container_name: db
volumes:
- "/home/data/pgdata:/var/lib/postgresql/data"
restart: always
我只安装了客户端:
sudo apt-get install -y postgresql-client
我在ubuntu上安装了psql客户端,我尝试连接到它,但似乎无法成功连接。

psql -h db -p 5432 -U postgres

psql: could not translate host name"db" to address: Temporary failure in name resolution


我尝试了其他主机,例如localhost,127.0.0.1和docker_db_1,但它们均无法正常工作。

最佳答案

您没有发布任何端口,因此您将无法从主机进行连接。您需要发布端口以从主机连接容器。

db:
image: postgres:9.4
container_name: db
ports:
- "5432:5432"

另外,如果尝试从主机连接,则应使用 localhost,DB仅在docker-compose网络中可访问
psql -h 127.0.0.1 -p 5432 -U postgres

或者您也可以验证容器内部
db:
image: postgres
container_name: db
ports:
- "5432:5432"

检查与在主机上安装客户端的连接
docker exec -it db bash -c "psql -U postgres"

关于postgresql - 连接到通过ubuntu服务器上的docker运行的postgres实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58126809/

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