[Fixed]-Python database WITHOUT using Django (for Heroku)

3👍

I’d use MongoDB. Heroku has support for it, so I think it will be really easy to start and scale out: https://addons.heroku.com/mongohq

About Python: MongoDB is a really easy database. The schema is flexible and fits really well with Python dictionaries. That’s something really good.

You can use PyMongo

from pymongo import Connection
connection = Connection()

# Get your DB
db = connection.my_database

# Get your collection
cars = db.cars

# Create some objects
import datetime
car = {"brand": "Ford",
       "model": "Mustang",
       "date": datetime.datetime.utcnow()}

# Insert it
cars.insert(car)

Pretty simple, uh?

Hope it helps.

EDIT:

As Endophage mentioned, another good option for interfacing with Mongo is mongoengine. If you have lots of data to store, you should take a look at that.

21👍

You can get a database provided from Heroku without requiring your app to use Django. To do so:

heroku addons:add heroku-postgresql:dev

If you need a larger more dedicated database, you can examine the plans at Heroku Postgres

Within your requirements.txt you’ll want to add:

psycopg2

Then you can connect/interact with it similar to the following:

import psycopg2
import os
import urlparse

urlparse.uses_netloc.append('postgres')
url = urlparse.urlparse(os.environ['DATABASE_URL'])

conn = psycopg2.connect("dbname=%s user=%s password=%s host=%s " % (url.path[1:], url.username, url.password, url.hostname))
cur = conn.cursor()

query = "SELECT ...."
cur.execute(query)

3👍

I did this recently with Flask. (https://github.com/HexIce/flask-heroku-sqlalchemy).

There are a couple of gotchas:

1. If you don’t use Django you may have to set up your database yourself by doing:

heroku addons:add shared-database

(Or whichever database you want to use, the others cost money.)

2. The database URL is stored in Heroku in the “DATABASE_URL” environment variable.
In python you can get it by doing.

dburl = os.environ['DATABASE_URL']

What you do to connect to the database from there is up to you, one option is SQLAlchemy.

👤chromy

0👍

Create a standalone Heroku Postgres database. http://postgres.heroku.com

👤Will

Leave a comment