我正在尝试在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中的
localhost
替换为ec2-23-21-215-184.compute-1.amazonaws.com
,但是出现了相同的错误。这里是执行db.create_all()时完整的回溯信息。请问您有什么想法,为什么连接会被拒绝以及如何解决这个问题?
heroku run bash
打开的bash中输入该代码。服务器不会将我给予bash的命令视为内部连接吗?我认为“外部”是指如果我在本地运行脚本并指向远程数据库。 - multigoodverse