gpt4 book ai didi

python - Heroku 拒绝连接到 postgres 数据库

转载 作者:行者123 更新时间:2023-11-29 12:26:01 24 4
gpt4 key购买 nike

我正在尝试在 Heroku 上部署基于 PostgreSQL 的 Flask 应用程序,但是当我尝试使用 db.create_all() 创建表时,我不断收到此错误:

(psycopg2.OperationalError) could not connect t
rver: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?

这是我的代码的样子:

app=Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI']='postgres://kfgriimpfjecsv:Bk1*****G@localhost:5432/dd71doth8gopgh'
db=SQLAlchemy(app)
if __name__ == '__main__':
app.debug=True
app.run()

我正在使用 Heroku Toolbelt 与 Heroku 服务器通信。另外,当我尝试这个时:

psql -p 5432 -h localhost

我得到几乎相同的错误:

psql: 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?

我还尝试了 ec2-23-21-215-184.compute-1.amazonaws.com 而不是 URI 中的 localhost,但得到了同样的错误。

here是我运行 db.create_all() 时得到的完整回溯:

知道为什么连接被拒绝以及如何解决这个问题吗?

最佳答案

看来我必须将 sslmode 参数附加到数据库 URI,因此将第二行更改为以下内容解决了问题:

app.config['SQLALCHEMY_DATABASE_URI']= "postgresql+psycopg2://kfgriimpfjecsv:Bk1*******G@ec2-23-21-215-184.compute-1.amazonaws.com:5432/dd71doth8gopgh?sslmode=require"

关于python - Heroku 拒绝连接到 postgres 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36726735/

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