[英]Connection refused to Postgresql on docker
我試圖通過psycopg2連接到postgres docker容器,但我一直得到同樣的錯誤。
我在jupyter(docker container)上做這個,我重啟了幾次postgres容器,我把postgresql.config listen_addresses = '*'
更改為listen_addresses = 'localhost'
,這是同樣的錯誤。
這是docker run命令:
docker run --name postgres -e POSTGRES_PASSWORD=xxxxxxx -d -p 5432:5432 -v $HOME/docker/volumes/postgres:/var/lib/postgresql/data postgres
蟒蛇
import psycopg2 as pg
connection = pg.connect("host=localhost dbname=easy_cleaning user=root")
我希望連接,但我收到此錯誤:
----> 3 connection = pg.connect("host=localhost dbname=easy_cleaning user=root")
/opt/conda/lib/python3.7/site-packages/psycopg2/__init__.py in connect(dsn, connection_factory, cursor_factory, **kwargs)
124
125 dsn = _ext.make_dsn(dsn, **kwargs)
--> 126 conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
127 if cursor_factory is not None:
128 conn.cursor_factory = cursor_factory
OperationalError: 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: Cannot assign requested address
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
在您的配置上,您的容器不在localhost上,Docker為其創建了一個私有IP。 所以你需要運行:
docker inspect postgres
並查找要在您的連接中使用的IPAddress字段:
connection = pg.connect("host=<DOCKER_IP_ADDRESS> dbname=easy_cleaning user=root")
要么
您可以使用docker-compose將圖像作為服務運行,每個服務都有它的名稱,可以在連接中用作主機名,例如:
connection = pg.connect("host=postgres dbname=easy_cleaning user=root")
在這里閱讀更多
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.