我正在尝试使用 SQLAlchemy 与 dockerized PostgreSQL 服务器进行交互。就像是:
engine = create_engine('postgresql://user:user_password@localhost:5432/database')
df.to_sql('table', engine)
Run Code Online (Sandbox Code Playgroud)
这给了我这个错误:
OperationalError: (psycopg2.OperationalError) 无法连接到服务器:连接被拒绝 服务器是否在主机“localhost”(::1) 上运行并接受端口 5432 上的 TCP/IP 连接?无法连接到服务器:连接被拒绝 服务器是否在主机“localhost”(127.0.0.1) 上运行并接受端口 5432 上的 TCP/IP 连接?
这表明 Docker postgresql(正在运行)在该端口不可用。我试过添加-p 5432:5432到我的docker-compose exec没有成功。有小费吗?