1. Home
  2. Computing & Technology
  3. Python

Reading from a PostgreSQL Database With Python

By Al Lukaszewski, About.com

2 of 9

Psycopg and Python: Perfect Together

The module we will use for this tutorial is again psycopg. It is available from http://www.initd.org/projects/psycopg1. So download it and install it using the directions that come with the package.

Once it is installed, you can import it like any other module:

# libs for database interface
import psycopg

If you would like your program to take input from the keyboard, you will also want to import the sys module. In this tutorial, I will illustrate how every major part of the SQL statement can be input from the keyboard. So, let your import line read as follows:

import sys, psycopg

2 of 9

Explore Python

More from About.com

  1. Home
  2. Computing & Technology
  3. Python
  4. Database Programming
  5. Reading PostgreSQL With Python - Psycopg and Python: Perfect Together

©2008 About.com, a part of The New York Times Company.

All rights reserved.