Psycopg2 is a widely used PostgreSQL driver for Python implementing DB API 2.0. It wraps libpq for efficient threaded access, cursors, COPY, and async notifications.
Overview
Psycopg2 connects Python applications to PostgreSQL using a C extension built on libpq. It implements the Python DB API 2.0 specification and is designed for multi-threaded programs that create many cursors and concurrent inserts or updates while sharing connections safely.
The driver supports client-side and server-side cursors, asynchronous communication, listeners for notifications, and COPY TO and COPY FROM workflows. Many Python types map to native PostgreSQL types out of the box, and the adaptation system can be extended for custom types. Unicode and Python 3 are supported in the maintained 2.x line.
Installation is typically via pip as psycopg2 or the prebuilt psycopg2-binary wheel for development and testing; production guidance favors building from source when compilers and libraries are available. Documentation ships with the project and online. The maintainers note that new features land in Psycopg 3 while psycopg2 remains actively maintained for existing codebases.
Applications import psycopg2, open connections with standard parameters, run SQL through cursors, and manage transactions explicitly. It is a client library, not a database server, and pairs with any PostgreSQL deployment you already host.
Key features
- Python DB API 2.0 implementation
- Thread-safe shared connections
- Client- and server-side cursors
- Asynchronous communication support
- PostgreSQL notification listening
- COPY TO and COPY FROM support
- Built-in Python to SQL type adaptation
- Unicode and Python 3 compatibility
Use cases
- Python web apps talking to PostgreSQL
- ETL scripts using COPY bulk load
- Long-running services with threaded workers
- Listening for database notification events
- Legacy projects staying on psycopg2