psycopg2

The most popular PostgreSQL database adapter for Python, implementing the full DB API 2.0 specification.

Library
PyPI
v2.9.13
3,657 stars
LGPL-3.0

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
70 /100 Good
Development Activity 68
Maintenance 32
Community 80
Maturity 60
Momentum 40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
66 /100 Good
Architecture 75
Code Quality 80
Innovation 55
Learning Curve 55

psycopg2 is a PostgreSQL database adapter for Python, implemented mostly in C as a direct wrapper around libpq for efficiency and stability. It provides a complete implementation of the Python DB API 2.0 specification, along with thread safety that lets multiple threads share the same connection — a design aimed squarely at heavily multi-threaded applications that create and destroy large numbers of cursors and issue many concurrent INSERTs or UPDATEs.

Beyond the core DB API surface, psycopg2 adds PostgreSQL-specific extensions: client-side and server-side (named) cursors, asynchronous communication and LISTEN/NOTIFY support, native COPY TO/COPY FROM for fast bulk data transfer, and a flexible, extensible object-adaptation system for mapping Python types to PostgreSQL types and back. It underpins a large share of the Python-PostgreSQL ecosystem, including as the default driver behind Django’s ORM and a common companion to SQLAlchemy. The project is stable and still actively maintained, though its own documentation notes it is not expected to receive new features, with Psycopg 3 positioned as where new development happens.

What You Get

  • DB API 2.0 compliant connection and cursor objects with the standard exception hierarchy (Error, DataError, IntegrityError, OperationalError, etc.)
  • Thread-safe connections that can be shared across multiple threads for heavily concurrent workloads
  • Client-side and server-side (named) cursors, including support for streaming large result sets
  • Native COPY TO/COPY FROM support for fast bulk import and export of data
  • Asynchronous execution and PostgreSQL LISTEN/NOTIFY support for event-driven applications
  • A connection pool implementation (psycopg2.pool) and an extensible type-adaptation system for custom Python-to-PostgreSQL type mapping

Common Use Cases

  • Backend database access for Python web frameworks (Django’s ORM, SQLAlchemy, Flask apps) talking to PostgreSQL
  • High-concurrency services that spawn many threads to run concurrent INSERTs/UPDATEs against a shared connection
  • Bulk ETL and data-pipeline jobs that use COPY FROM/COPY TO to move data quickly between files and PostgreSQL tables
  • Realtime, event-driven applications that use PostgreSQL LISTEN/NOTIFY for pub/sub-style updates

Under The Hood

Architecture

psycopg2 is a hybrid C/Python library: the core C extension module (built from the files in psycopg/, e.g. connection_type.c, cursor_type.c, and the adapter_*.c files) implements connection, cursor, and type-adaptation machinery directly against libpq, while the pure-Python layer in lib/ (__init__.py, extensions.py, extras.py, pool.py, sql.py, errors.py) re-exports the C primitives, registers default type adapters, and layers DB API 2.0 ergonomics — connect(), register_adapter, connection pooling — on top. This thin-Python/thick-C split puts the wire protocol and object lifecycle in C while Python owns the public API surface, exception hierarchy, and pluggable adaptation registry, meaning changes to the core adapter/typecast abstraction ripple across both language boundaries.

Tech Stack

Built with setuptools/distutils and compiled as a CPython extension against libpq, PostgreSQL’s C client library, with no external Python runtime dependencies beyond the standard library. It supports a wide span of Python versions (3.9 through 3.14) across Linux, macOS, and Windows, with packaging spanning C, Python, Makefile, and shell script. There is no bundled web framework or ORM — psycopg2 sits underneath tools like Django and SQLAlchemy as the driver layer.

Code Quality

The test suite is extensive, including a full DB API 2.0 compliance suite alongside dozens of focused test modules covering connections, cursors, COPY, async operations, notifications, replication, and type handling, all run against real PostgreSQL instances in CI rather than mocks. Error handling follows the DB API 2.0 typed exception hierarchy surfaced from the C layer, giving callers structured, catchable failure modes. CI exercises a broad matrix of Python and PostgreSQL version combinations on every push and pull request. The Python layer has no visible type annotations or static-analysis tooling in CI, which is the main gap relative to a fully modern codebase.

What Makes It Unique

psycopg2’s differentiation is being a C-level libpq wrapper purpose-built for heavily multi-threaded, cursor-churning workloads: thread-safe connection sharing, an extensible C-level type-adaptation system, and native COPY and LISTEN/NOTIFY support that many pure-Python drivers only bolt on later. It deliberately implements the DB API 2.0 contract precisely rather than reinventing it, so its edge is performance and protocol completeness rather than novel API design — the project itself is now positioned as stable and feature-frozen, with new development directed toward Psycopg 3.

Used by 42 apps in this directory

Python
66%
MIT

Flowfile

Data Engineering

398

Visual ETL that compiles to Polars — build pipelines on a canvas, export as standalone Python, and run anywhere without platform lock-in.

View details
83
Repo Health
81
Technical
66
Dependency
Built with
Python 66%
TypeScript 17%
Vue 16%
Updated 1 weeks ago
TypeScript
53%
Apache 2.0

Flowsint

Automation · Developer Tools

9,911

A privacy-first, graph-based OSINT investigation platform with 30+ automated enrichers for mapping relationships between domains, IPs, people, and organizations.

View details
85
Repo Health
71
Technical
67
Dependency
Built with
TypeScript 53%
Python 45%
Updated 1 weeks ago
TypeScript
98%
Other

GrowthBook

Ab Testing Experimentation · Analytics · Developer Tools

8,527

Open source feature flags, A/B testing, and warehouse-native experimentation that queries your existing data infrastructure—no data movement required.

View details
92
Repo Health
85
Technical
63
Dependency
Built with
TypeScript 98%
Updated 6 days ago
HTML
45%
LGPL-2.1

Horilla

ERP · Human Resources

1,470

Open-source HRMS covering recruitment, attendance, payroll, and biometrics in one self-hosted Django application.

View details
91
Repo Health
60
Technical
64
Dependency
Built with
HTML 45%
Python 41%
JavaScript 11%
Updated 1 weeks ago
Python
64%
Other

Keep

Automation · Devops · Monitoring

12,433

The open-source AIOps and alert management platform that unifies 130+ monitoring tools into a single pane of glass with AI-powered correlation, deduplication, and workflow automation.

View details
88
Repo Health
79
Technical
66
Dependency
Built with
Python 64%
TypeScript 36%
Updated 1 weeks ago
Python
51%
AGPL 3.0

Khoj

AI Assistants · Knowledge Management · Productivity

37,802

A self-hostable AI second brain that chats with your documents, searches the web, builds custom agents, and runs entirely on your own LLM.

View details
62
Repo Health
82
Technical
66
Dependency
Built with
Python 51%
TypeScript 36%
Updated 2 months ago
Python
94%
Apache 2.0

knowhere

AI Development · AI Memory · Developer Tools

3,755

Transform messy, unstructured documents into persistent, navigable memory that AI agents can actually use.

View details
83
Repo Health
75
Technical
66
Dependency
Built with
Python 94%
Updated 6 days ago
Python
53%
AGPL 3.0

LearnHouse

CMS · Learning Management

2,413

Open-source LMS with AI tutoring, real-time collaboration boards, live code execution, and built-in course monetization — self-hosted in minutes.

View details
90
Repo Health
77
Technical
65
Dependency
Built with
Python 53%
TypeScript 45%
Updated 6 days ago
C++
66%
Other

Memgraph

AI Development · Databases

4,628

High-performance in-memory graph database for AI context and real-time analytics

View details
90
Repo Health
79
Technical
66
Dependency
Built with
C++ 66%
Python 18%
Updated 6 days ago

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers