简体   繁体   English

在 Flask 应用程序的 Heroku 上运行 manage.py 会给出“无法连接”

[英]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.

相关问题 在 Dockerfile 中运行“manage.py compilemessages”给出“django.db.utils.OperationalError:无法连接到服务器:没有这样的文件或目录” - Running “manage.py compilemessages” in Dockerfile gives “django.db.utils.OperationalError: could not connect to server: No such file or directory” 在Heroku上运行Django自定义manage.py任务-导入问题 - Running Django custom manage.py task on Heroku - Importing Issues 在Django应用中运行manage.py操作的选项 - Options for running manage.py actions within a django app heroku运行python manage.py collectstatic - heroku run python manage.py collectstatic Heroku上的Django manage.py自定义命令 - Django manage.py custom command on Heroku Heroku无法识别Python Manage.py命令 - Python Manage.py Commands Not Recognized on Heroku Django Manage.py不提供南方命令 - Django Manage.py Gives No South Commands 在本地运行“ python manage.py syncdb”时出错,但通过Heroku运行相同的命令时没有错误 - Error when running “python manage.py syncdb” locally, but no error when running the same command via Heroku 无法在Pycharm中执行“ manage.py”命令 - could not execute “manage.py”command in Pycharm Heroku教程错误运行python manage.py syncdb更新本地数据库 - Heroku tutorial error running python manage.py syncdb to update local database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM