Sunday, August 02, 2009

How to connect to a PostgreSQL database using Python on Ubuntu Linux

% sudo apt-get install python-psycopg2
% python
Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
>>> conn = psycopg2.connect("host='localhost' dbname='satoshiabe' user='satoshiabe' password='satoshiabe'");
>>> cur = conn.cursor()
>>> cur.execute("""SELECT * FROM table01""")
>>> rows = cur.fetchall()
>>> for row in rows:
...        print row

No comments: