【发布时间】:2018-07-24 01:31:56
【问题描述】:
我使用 Docker 和以下命令在我的计算机上设置了一个临时 Postgres 数据库用于测试目的:
1)
sudo docker run --name some-postgres6 -e POSTGRES_PASSWORD=mysecretpassword -p 5430:5432 postgres:9.1 -d postgres
2)
sudo docker run -it --rm --link some-postgres6:postgres postgres psql -h postgres -U postgres
我想使用 Python 连接到数据库:
from sqlalchemy.engine import create_engine
import psycopg2
engine = create_engine('postgresql+psycopg2://postgres:mysecretpassword@localhost/mydb?port=5432') ### IMPORTANT!!!
connection = engine.connect()
然后我收到此错误:
psycopg2.OperationalError: could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
这段代码曾经在我运行 Ubuntu 的另一台机器上运行。我想这与windows有关。我正在运行 Windows 10 Home(没有 Windows 经验)并使用 Docker 工具箱。
【问题讨论】:
-
你能从
psql连接吗? -
从 psql 工作连接
-
那是 Windows,您查看过您的防火墙/防病毒日志吗?他们可以有足够的眼光让
psql.exe连接,但阻止python.exe。 -
不,防病毒软件没有阻止任何东西
标签: python postgresql docker sqlalchemy