jdotjdot
jdotjdot

Reputation: 17052

Python database WITHOUT using Django (for Heroku)

To my surprise, I haven't found this question asked elsewhere. Short version, I'm writing an app that I plan to deploy to the cloud (probably using Heroku), which will do various web scraping and data collection. The reason it'll be in the cloud is so that I can have it be set to run on its own every day and pull the data to its database without my computer being on, as well as so the rest of the team can access the data.

I used to use AWS's SimpleDB and DynamoDB, but I found SDB's storage limitations to be to small and DDB's poor querying ability to be a problem, so I'm looking for a database system (SQL or NoSQL) that can store arbitrary-length values (and ideally arbitrary data structures) and that can be queried on any field.

I've found many database solutions for Heroku, such as ClearDB, but all of the information I've seen has shown how to set up Django to access the database. Since this is intended to be script and not a site, I'd really prefer not to dive into Django if I don't have to.

Is there any kind of database that I can hook up to in Heroku with Python without using Django?

Upvotes: 14

Views: 4455

Answers (4)

CraigKerstiens
CraigKerstiens

Reputation: 5954

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)

Upvotes: 21

santiagobasulto
santiagobasulto

Reputation: 11686

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.

Upvotes: 3

chromy
chromy

Reputation: 162

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.

Upvotes: 3

Will
Will

Reputation: 2951

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

Upvotes: 0

Related Questions