[英]Running manage.py on Heroku for Flask app gives “could not connect”
I am trying to migrate my database on Heroku using heroku run python manage.py db migrate
on my Flask app.我正在尝试使用
heroku run python manage.py db migrate
在我的 Z9784E91C7B265789172 应用程序上。 But I am getting this error:但我收到此错误:
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) 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?
Here is the code to my manage.py
file:这是我的
manage.py
文件的代码:
import os
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from app import create_app, db
app=create_app()
with app.app_context():
migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command('db', MigrateCommand)
if __name__ == '__main__':
manager.run()
Here is the configuration file to the database:这是数据库的配置文件:
import os
class Config:
SECRET_KEY= os.environ.get('SECRET_KEY')
SQLALCHEMY_DATABASE_URI= os.environ.get('SQLALCHEMY_DATABASE_URI')
MAIL_SERVER='smtp.googlemail.com'
MAIL_PORT=587
MAIL_USE_TLS = True
MAIL_USERNAME= os.environ.get('EMAIL_USER')
MAIL_PASSWORD= os.environ.get('EMAIL_PASS')
I have set up the sqlalchemy_database_uri
to a database in PostgreSQL in this format postgres://YourUserName:YourPassword@YourHost:5432/YourDatabase
.我已将 sqlalchemy_database_uri 设置为
sqlalchemy_database_uri
中的数据库,格式为postgres://YourUserName:YourPassword@YourHost:5432/YourDatabase
。 This error has been bugging me and I cannot find a solution anywhere.这个错误一直困扰着我,我无法在任何地方找到解决方案。
Why isn't this working?为什么这不起作用?
You can't connect your Heroku application to your local database without some major drawbacks.您无法将 Heroku 应用程序连接到本地数据库而没有一些主要缺点。 It's a huge pain to set up (you'll need to deal with port forwarding and likely at least one firewall, probably dynamic IP addresses, ...) and your application won't run if your laptop is off.
设置起来非常痛苦(您需要处理端口转发,可能至少需要一个防火墙,可能是动态 IP 地址,...)如果您的笔记本电脑关闭,您的应用程序将无法运行。
A better solution would be to have a database in the cloud, and given that you are already using Heroku and PostgreSQL the most natural choice is Heroku's own Postgres service .更好的解决方案是在云中拥有一个数据库,并且鉴于您已经在使用 Heroku 和 PostgreSQL ,最自然的选择是Heroku 自己的 Postgres 服务。 If you're depending on
psycopg2
, there's a good chance that one has already been provisioned for you.如果您依赖
psycopg2
,很有可能已经为您配置了一个。
If it has, you'll see a DATABASE_URL
environment variable containing your connection string.如果有,您将看到一个包含连接字符串的
DATABASE_URL
环境变量。 Simply set your SQLAlchemy database URI from that:只需从中设置您的 SQLAlchemy 数据库 URI:
SQLALCHEMY_DATABASE_URI= os.environ.get('DATABASE_URL')
If a database hasn't been provisioned for you (check by running heroku addons
) you can provision one using the free tier easily:如果尚未为您配置数据库(通过运行
heroku addons
进行检查),您可以使用免费层轻松配置一个:
heroku addons:create heroku-postgresql:hobby-dev
Note that you shouldn't be running manage.py db migrate
on Heroku at all.请注意,您根本不应该在 Heroku 上运行
manage.py db migrate
。 This generates migration files, which will be lost on Heroku's ephemeral filesystem .这会生成迁移文件,这些文件将在Heroku 的临时文件系统上丢失。 Generate migrations locally , and commit the migration files.
在本地生成迁移,并提交迁移文件。 You'll want to run
manage.py db upgrade
in both places, though.不过,您需要在这两个地方运行
manage.py db upgrade
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.