How to use Flask/Peewee with Heroku? How to use Flask/Peewee with Heroku? flask flask

How to use Flask/Peewee with Heroku?


Are you parsing the DATABASE_URL environment variable? It will look something like this:

postgres://username:password@host:port/database_name

So you will want to pull that in and parse it before you open a connection to your database. Depending on how you've declared your database (in your config or next to your wsgi app) it might look like this:

import osimport urlparseurlparse.uses_netloc.append('postgres')url = urlparse.urlparse(os.environ['DATABASE_URL'])# for your configDATABASE = {    'engine': 'peewee.PostgresqlDatabase',    'name': url.path[1:],    'password': url.password,    'host': url.hostname,    'port': url.port,}

See the notes here: https://devcenter.heroku.com/articles/django


heroku config:set HEROKU=1

import osimport urlparseimport psycopg2from flask import Flaskfrom flask_peewee.db import Databaseif 'HEROKU' in os.environ:    DEBUG = False    urlparse.uses_netloc.append('postgres')    url = urlparse.urlparse(os.environ['DATABASE_URL'])    DATABASE = {        'engine': 'peewee.PostgresqlDatabase',        'name': url.path[1:],        'user': url.username,        'password': url.password,        'host': url.hostname,        'port': url.port,    }else:    DEBUG = True    DATABASE = {        'engine': 'peewee.PostgresqlDatabase',        'name': 'framingappdb',        'user': 'postgres',        'password': 'postgres',        'host': 'localhost',        'port': 5432 ,        'threadlocals': True    }app = Flask(__name__)app.config.from_object(__name__)db = Database(app)

Modified coleifer's answer to answer hasenj's comment. Please mark one of these as the accepted answer.


According to the Peewee docs, you don't want to use Proxy() unless your local database driver is different than your remote one (i.e. locally, you're using SQLite and remotely you're using Postgres). If, however, you are using Postgres both locally and remotely it's a much simpler change. In this case, you'll want to only change the connection values (database name, username, password, host, port, etc.) at runtime and do not need to use Proxy().

Peewee has a built-in URL parser for database connections. Here's how to use it:

import osfrom peewee import *from playhouse.db_url import connectdb = connect(os.environ.get('DATABASE_URL'))class BaseModel(Model):    class Meta:        database = db

In this example, peewee's db_url module reads the environment variable DATABASE_URL and parses it to extract the relevant connection variables. It then creates a PostgresqlDatabase object with those values.

Locally, you'll want to set DATABASE_URL as an environment variable. You can do this according to the instructions of whatever shell you're using. Or, if you want to use the Heroku toolchain (launch your local server using heroku local) you can add it to a file called .env at the top level of your project. For the remote setup, you'll want to add your database URL as a remote Heroku environment variable. You can do this with the following command:

heroku config:set DATABASE_URL=postgresql://myurl

You can find that URL by going into Heroku, navigating to your database, and clicking on "database credentials". It's listed under URI.