Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heroku /Flask/Postgres: Can not connect to server

I am new to flask/Heroku and deploying an app using postgres/sqlalchemy. I am using flask-migrate (built on alembic) for db migrations. Everythign works fine locally even when starting with foreman but i am unable to get things running on the Heroku server. I believe it has something to do with the DB connection and and Flask-migrate but im not sure. Been at this for hours and and scoured SO to no avail. I know im making a stupid mistake.

Error before Installing 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?

Error after Heroku Postgres install -

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?

Here is my project structure

myproject/
    -app
        -__init__.py
        -forms.py
        -helper.py
        -views.py
        -models.py
        -static/
        -templates/
     -config.py
     -run.py
     -Procfile
     -requirements.txt
     -migrations/

Here is my 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

Here is my config.py file:

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 last but not least:

web: gunicorn app:app
like image 785
user2957824 Avatar asked Oct 01 '22 19:10

user2957824


1 Answers

You have a typo when you choose the database url:

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

instead of

if os.environ.get('DATABASE_URL') is None:
like image 187
fasouto Avatar answered Oct 18 '22 18:10

fasouto