Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 600
jdotjdot Avatar asked May 17 '12 17:05

jdotjdot


1 Answers

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)
like image 177
CraigKerstiens Avatar answered Oct 14 '22 19:10

CraigKerstiens