Python/PostgreSQL Interface Architecture

PostgreSQL usually comes packaged with a Python interface named PyGreSQL. PyGreSQL is a small collection of classes and functions that enable a Python application to interact with a PostgreSQL database. For the last several releases, PyGreSQL has included an alternate interface, which we will call the DB-API. The DB-API was designed to offer a portable interface between Python applications and a variety of relational databases. In the case of PostgreSQL, the DB-API is distributed as a wrapper around the PyGreSQL interface. So, when your Python application calls a DB-API function, the DB-API layer translates the request into a PyGreSQL function call. The results from each DB-API function call are translated from PyGreSQL form back into DB-API form and then returned to your application.

In addition to the PyGreSQL interface (and the PyGreSQL/DB-API wrapper), there are at least two other implementations of the DB-API written for PostgreSQL. The first, PsycoPg (isn't that a great name?), can be found at The second, PoPy lives at All three of these interfaces are DB-API-compliant?that's good news because you can switch between implementations without major changes to your application.

The alternate implementations (PyscoPg and PoPy) have been designed to maximize performance. The interface distributed with PostgreSQL (PyGreSQL) was implemented as a wrapper, so it probably won't be quite as fast as the other two; but with PyGreSQL, you can pick and choose between the two interface layers.

In this chapter, I'll describe the DB-API interface between Python and PostgreSQL, but not PyGreSQL. Applications written to the DB-API specification can connect to different databases; applications written using PyGreSQL cannot. The PostgreSQL Programmer's Guide contains a description of the underlying (or alternate, depending on your perspective) PyGreSQL interface.

    Part II: Programming with PostgreSQL