Unsupported versions: 7.3 / 7.2 / 7.1
This documentation is for an unsupported version of PostgreSQL.
You may want to view the same page for the current version, or one of the other supported versions listed above instead.

Chapter 6. PyGreSQL - Python Interface

Table of Contents
6.1. The pg Module
6.2. pg Module Functions
connect -- open a connection to the database server
get_defhost -- get default host name [DV]
set_defhost -- set default host name [DV]
get_defport -- get default port [DV]
set_defport -- set default port [DV]
get_defopt -- get default options specification [DV]
set_defopt -- set default options specification [DV]
get_deftty -- get default connection debug terminal specification [DV]
set_deftty -- set default connection debug terminal specification [DV]
get_defbase -- get default database name specification [DV]
set_defbase -- set default database name specification [DV]
6.3. Connection Object: pgobject
query -- execute a SQL command
reset -- reset the connection
close -- close the database connection
fileno -- return the socket used to connect to the database
getnotify -- get the last notify from the server
inserttable -- insert a list into a table
putline -- write a line to the server socket [DA]
getline -- get a line from server socket [DA]
endcopy -- synchronize client and server [DA]
locreate -- create a large object in the database [LO]
getlo -- build a large object from given OID [LO]
loimport -- import a file to a PostgreSQL large object [LO]
6.4. Database Wrapper Class: DB
pkey -- return the primary key of a table
get_databases -- get list of databases in the system
get_tables -- get list of tables in connected database
get_attnames -- return the attribute names of a table
get -- get a tuple from a database table
insert -- insert a tuple into a database table
update -- update a database table
clear -- clear a database table
delete -- delete a row from a table
6.5. Query Result Object: pgqueryobject
getresult -- get the values returned by the query
dictresult -- get the values returned by the query as a list of dictionaries
listfields -- list the fields names of the query result
fieldname -- get field name by number
fieldnum -- get field number by name
ntuples -- return the number of tuples in query object
6.6. Large Object: pglarge
open -- open a large object
close -- close the large object
read -- read from the large object
write -- write to the large object
seek -- change current position in the large object
tell -- return current position in the large object
unlink -- delete the large object
size -- return the large object size
export -- save the large object to file

Author: Written by D'Arcy J.M. Cain (). Based heavily on code written by Pascal Andre . Copyright © 1995, Pascal Andre. Further modifications Copyright © 1997-2000 by D'Arcy J.M. Cain.

You may either choose to use the old mature interface provided by the pg module or otherwise the newer pgdb interface compliant with the DB-API 2.0 specification developed by the Python DB-SIG.

Here we describe only the older pg API. As long as PyGreSQL does not contain a description of the DB-API you should read about the API at http://www.python.org/topics/database/DatabaseAPI-2.0.html.

A tutorial-like introduction to the DB-API can be found at http://www2.linuxjournal.com/lj-issues/issue49/2605.html

6.1. The pg Module

The pg module defines three objects:

  • pgobject, which handles the connection and all the requests to the database,

  • pglargeobject, which handles all the accesses to PostgreSQL large objects, and

  • pgqueryobject that handles query results.

If you want to see a simple example of the use of some of these functions, see http://www.druid.net/rides where you can find a link at the bottom to the actual Python code for the page.

6.1.1. Constants

Some constants are defined in the pg module dictionary. They are intended to be used as a parameters for methods calls. You should refer to the libpq description (Chapter 1) for more information about them. These constants are:

INV_READ
INV_WRITE

large objects access modes, used by (pgobject.)locreate and (pglarge.)open.

SEEK_SET
SEEK_CUR
SEEK_END

positional flags, used by (pglarge.)seek.

version
__version__

constants that give the current version