Heroku拒绝连接到postgres数据库

时间:2016-04-19 18:35:54

标签: python postgresql ssl heroku

我正在尝试在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?

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

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

知道连接被拒绝的原因以及如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

似乎我必须将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"
相关问题