我使用 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 10 Home(没有 Windows 经验)并使用 Docker 工具箱。
我终于弄清楚了这个问题。它是 ”本地主机“,127.0.0.1 上没有任何运行。
我必须将其更改为 docker 机器的 IP。如果您打开此信息,则会显示Docker 快速入门终端。它显示类似“docker is configured to use the default machine with IP 192.168.XX.XXX
"
找到这个IP的另一种方法是打开资源监控器,转到Network选项卡,然后检查TCP 连接数。应该有docker.exe
运行中。IP显示在远程地址专栏是可行的。
最后是正确的命令:
engine = create_engine('postgresql+psycopg2://postgres:[email protected]/mydb?port=5430')
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)