Latest articles

Psycopg 2.7.3 released

Posted by Daniele Varrazzo on July 24, 2017
Tagged as news, release

A quick release to fix a regression found in psycopg 2.7.2:

  • Restored default timestamptz[] typecasting to Python datetime. Regression introduced in Psycopg 2.7.2 (ticket #578).


Psycopg 2.7.2 released

Posted by Daniele Varrazzo on July 22, 2017
Tagged as news, release

Releasing psycopg2 version 2.7.2: a release fixing a host of bugs found in the last 3-4 months.


Psycopg 2.7.1 released

Posted by Daniele Varrazzo on March 13, 2017
Tagged as news, release

A quick bugfix release to solve some teething problems with some of the changes introduced in 2.7:

  • Ignore None arguments passed to connect() and make_dsn() (ticket #517).
  • OpenSSL upgraded from major version 0.9.8 to 1.0.2 in the Linux wheel packages (ticket #518).
  • Fixed build with libpq versions < 9.3 (ticket #520).


Psycopg 2.7 released

Posted by Daniele Varrazzo on March 1, 2017
Tagged as news, release

Finally here! Thank you very much for waiting so long: we have finally released Psycopg 2.7!


  • Faster! Helps generating SQL for repeatedly executed statements and faster Unicode decoding.
  • Safer! Helps generating dynamic SQL statements with variable table and field names.
  • Easier! Use the binary package to avoid the need of C compiler, pg_config, libpq required on the clients.
  • Replication! Support for PostgreSQL physical and logical replication.
  • Plays-better-with-pgbouncer-at-transaction-pooling-level! This.


Psycopg 2.7 beta 2 released

Posted by Daniele Varrazzo on February 17, 2017
Tagged as news, release


we have released psycopg2 version 2.7 beta 2. This version comes two years after the previous major release so it is packed with new features and improvements; among the main points:

  • support for replication protocol
  • helpers to build dynamically SQL statements and manipulate connection strings
  • speedups in multiple execution and Unicode decoding
  • better transactions characteristics support

A more complete list of changes is available at

On top of the changes in the code we have addressed the deployment problems found my many inexperienced users, especially on Windows and OSX: psycopg is now distributed as a self-contained wheel package on PyPI, so that:

pip install psycopg2

will not require the presence of C compiler, headers, pg_config, libpq.

Because of all these changes, we ask you kindly to test this psycopg beta release, both its installation and its usage, before we release the stable version, which should happen next week if we don't find any problem. The binary packages of the beta release are on the PyPI testing site: you can install it using:

pip install -U pip # make sure to have an up-to-date pip
pip install -i psycopg2==2.7b2

Thank you very much everyone, and I wish to thank especially Oleksandr Shulgin and Craig Ringer for the development of the replication protocol support (the development of which brought along several other features and improvement) and Jason Erickson for the invaluable help with the Windows packages.

Happy testing!

Psycopg 2.6.2 released

Posted by Daniele Varrazzo on July 7, 2016
Tagged as news, release

Psycopg 2.6.2 has been released. You can get it from:

This is an interim release, packing together one year of bug fixes, before the release 2.7, intended to deliver several new features. Thank you very much to everybody contributing with reports, code, suggestions.


Psycopg 2.6.1 released

Posted by Daniele Varrazzo on June 16, 2015
Tagged as news, release

Psycopg 2.6.1 has been released. You can get it from:


Psycopg 2.6 and 2.5.5 released

Posted by Daniele Varrazzo on February 9, 2015

We have just released two Psycopg versions: 2.5.5 containing a few bug fixes and 2.6 introducing some new features.


Psycopg 2.5.4 released

Posted by Daniele Varrazzo on August 30, 2014
Tagged as news, release

Psycopg 2.5.4 has been released. You can get it from:

This version supports the new jsonb PostgreSQL type out-of-the-box. And of course there are a few bug fixed:


Cancelling PostgreSQL statements from Python

Posted by Daniele Varrazzo on July 20, 2014
Tagged as recipe

Cancelling a long running query from Python is not something that happens automatically: the libpq doesn't react to Python signals so the only way to stop a query is to run a pg_cancel_backend from another process. Killing the Python process won't cancel the query: it will run until completion and then rolled back. This makes working wth long-running query from the Python interpreter somewhat frustrating.

Using psycopg in green mode moves the waiting from the libpq C code to Python: this gives Python some chance of interaction: it is possible for instance to catch a ctrl-c and send a cancel request:

from select import select
from psycopg2.extensions import POLL_OK, POLL_READ, POLL_WRITE

def wait_select_inter(conn):
    while 1:
            state = conn.poll()
            if state == POLL_OK:
            elif state == POLL_READ:
                select([conn.fileno()], [], [])
            elif state == POLL_WRITE:
                select([], [conn.fileno()], [])
                raise conn.OperationalError(
                    "bad state from poll: %s" % state)
        except KeyboardInterrupt:
            # the loop will be broken by a server error


An interactive session would look like:

>>> cnn = psycopg2.connect('')
>>> cur = cnn.cursor()
>>> cur.execute("select pg_sleep(10)")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
QueryCanceledError: canceling statement due to user request

The connection is now in error state, but a cnn.rollback() would make it working again.