python-Heroku / Flask / Postgres:无法连接到服务器

我是flask / Heroku的新手,正在使用postgres / sqlalchemy部署应用程序.我正在使用flask-migrate(基于alembic构建)进行数据库迁移.即使从领班开始,Everythign在本地也能正常工作,但是我无法在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']

运行

#!/usr/bin/env python
from app import manager
manager.run()

最后但并非最不重要的是Procfile:

web: gunicorn app:app

解决方法:

选择数据库URL时出现错字:

if os.environ.get('DATABSE_URL') is None:

代替

if os.environ.get('DATABASE_URL') is None:
上一篇:Python编程:将Flask项目部署到免费服务器Heroku


下一篇:python-将花部署到Heroku