This project is archived and is in readonly mode.

#126 ✓invalid
Psycopg website

Possible bug in cursor iterator

Reported by Psycopg website | September 19th, 2012 @ 03:41 PM

Submitted by: Pietro Delsante <pietro.delsante at>


I have a script that uses psycopg2 2.4.5 (OS is Ubuntu 12.04, psycopg2 was installed from Ubuntu repository with apt-get) to open a connection to a local PostgreSQL database and another one to a remote PostgreSQL database, like that:"Connecting to local database...")

localconn = psycopg2.connect(host='localhost', dbname='somedb', user='someuser')
localcur = localconn.cursor(cursor_factory=psycopg2.extras.DictCursor)


logger.critical("Could not connect to local database, aborting!")
sys.exit(0)"Connecting to remote database...")

remoteconn = psycopg2.connect(host='somehost', dbname='somedb', user='someuser', password='somepassword')
remotecur = remoteconn.cursor(cursor_factory=psycopg2.extras.DictCursor)


logger.critical("Could not connect to remote database, aborting!")

Then, I try to execute a SELECT query with one of the two cursors, then I start a cycle iterating on the results, like that:

remotecur.execute("SELECT * FROM sometable;")
for remoteresult in remotecur:

# Do something...

When I try to do that with the cursor pointing to the REMOTE database, everything seems to work pretty well; however, if I do the very same with the LOCAL database, like that;

localcur.execute("SELECT * FROM sometable;")
for localresult in localcur:

# Do something...

... I get this exception:

me@myhost:/path/to/# ./
Traceback (most recent call last):
File "./", line 37, in

for localsample in localcur:

File "/usr/lib/python2.7/dist-packages/psycopg2/", line 101, in iter


psycopg2.ProgrammingError: no results to fetch

Of course, I checked that, in the LOCAL database, the query I am trying to execute does have a lot of results. In fact, if I modify my code to use fetchall() instead of using the iterator, like that:

localcur.execute("SELECT * FROM sometable;")
for localresult in localcur.fetchall():

# Do something...

... everything works pretty well, except that, of course, it's way slower.

Could this be a bug?

pietro.delsante at

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

<b>WARNING:</b> the informations in this tracker are archived. Please submit new tickets or comments to <a href="">the new tracker</a>.
Psycopg is the most used PostgreSQL adapter for the Python programming language. At the core it fully implements the Python DB API 2.0 specifications. Several extensions allow access to many of the features offered by PostgreSQL.

Shared Ticket Bins