我是一个新手,正在使用Flask/Heroku,并使用postgres/sqlalchemy部署应用程序。我使用了flask-migrate(基于alembic)进行数据库迁移。在本地甚至在使用foreman启动时一切都运行得很好,但我无法在Heroku服务器上使其正常运行。我认为这与数据库连接和Flask-migrate有关,但我不确定。我已经花费数小时时间,并在SO中搜寻了大量信息,但仍然无法解决问题。我知道自己犯了一个愚蠢的错误。
安装Heroku Postgres之前出现的错误 -
这是我的项目结构。
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?
安装Heroku Postgres后出现错误 -
2014-05-16T21:26:14.408879+00:00 app[web.1]: Is the server running on host "localhost" (127.0.0.1) and accepting
2014-05-16T21:26:14.408880+00:00 app[web.1]: TCP/IP connections on port 5432?
这是我的项目结构。
myproject/
-app
-__init__.py
-forms.py
-helper.py
-views.py
-models.py
-static/
-templates/
-config.py
-run.py
-Procfile
-requirements.txt
-migrations/
这是我的init.py文件:
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.script import Manager, Server
from flask.ext.migrate import Migrate, MigrateCommand
from flask.ext.admin import Admin
app = Flask(__name__)
app.config.from_object('config')
db = SQLAlchemy(app)
admin = Admin(app)
migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command('db', MigrateCommand)
from app import views, models, forms, helper
这是我的 config.py 文件:
import os
DEBUG = True
basedir = os.path.abspath(os.path.dirname(__file__))
CSRF_ENABLED = True
CSRF_SESSION_KEY = '**********'
ADMINS = frozenset(['myemail'])
SECRET_KEY = '*******'
if os.environ.get('DATABSE_URL') is None:
SQLALCHEMY_DATABASE_URI = 'postgresql+psycopg2://******@localhost/myproject'
else:
SQLALCHEMY_DATABASE_URI = os.environ['DATABASE_URL']
run.py
#!/usr/bin/env python
from app import manager
manager.run()
最后是 Procfile:
web: gunicorn app:app