The PyQGIS Programmer's Guide

Extending Quantum GIS with Python

Creating a PostgreSQL Connection From a QGIS Layer Datasource

Sometimes you may want to send SQL to your PostgreSQL database from a plugin or PyQGIS script. To do this, you need a connection to your database. Since QGIS uses its own low-level interface to PostgreSQL, it can’t provide a connection for general query use. You can, however, use the information from the QGIS PostgreSQL data provider to create your own connection using QSqlDatabase, a Qt class.