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
68%
Apache 2.0

otari

AI Development · Developer Tools

733

A self-hosted, OpenAI-compatible LLM gateway that puts one endpoint in front of 40+ providers, with virtual API keys, per-user budgets enforced before spend, and full usage tracking.

View details
82
Repo Health
88
Technical
74
Dependency
Built with
Python 68%
TypeScript 31%
Updated 1 weeks ago
TypeScript
58%
MIT

pad.ws

Collaboration · Design Tools

4,206

A browser-based whiteboard that doubles as a full development environment, letting you draw and code in the same canvas.

View details
37
Repo Health
61
Technical
78
Dependency
Built with
TypeScript 58%
Python 31%
SCSS 10%
Updated 1 years ago
TypeScript
50%
Other

Phase Console

Devops · Security

931

End-to-end encrypted secrets management for engineering teams — from local dev to Kubernetes production.

View details
85
Repo Health
73
Technical
63
Dependency
Built with
TypeScript 50%
Python 48%
Updated 1 weeks ago
Python
62%
Apache 2.0

Polar

Developer Tools · Ecommerce · Invoicing Finance

10,410

Open source payments infrastructure that turns software into a business — subscriptions, usage-based billing, digital products, and merchant-of-record compliance in one platform.

View details
90
Repo Health
82
Technical
66
Dependency
Built with
Python 62%
TypeScript 31%
Updated 1 weeks ago
Python
55%
Other

PostHog

Ab Testing Experimentation · Analytics · Developer Tools

40,397

The all-in-one open source product platform combining analytics, session replay, feature flags, error tracking, AI observability, and a built-in data warehouse in a single self-hostable stack.

View details
92
Repo Health
80
Technical
65
Dependency
Built with
Python 55%
TypeScript 36%
Updated 6 days ago
Python
87%
Apache 2.0

PrivateGPT

AI Development

57,611

The open-source API layer that turns local LLMs into production private AI applications with full Claude API compatibility

View details
88
Repo Health
83
Technical
70
Dependency
Built with
Python 87%
Updated 1 weeks ago
Python
99%
Apache 2.0

Rasa Open Source

AI Assistants · AI Development · Voice AI

21,353

Rasa Open Source is a Python machine learning framework for building contextual, multi-turn chatbots and voice assistants that understand natural language and maintain conversation state.

View details
62
Repo Health
78
Technical
63
Dependency
Built with
Python 99%
Updated 3 months ago
Python
45%
Other

Redash

Analytics · Data Engineering

28,849

Redash lets anyone connect to 35+ SQL and NoSQL data sources, write a query in the browser, and turn the result into a shared dashboard — no separate BI suite required.

View details
91
Repo Health
74
Technical
61
Dependency
Built with
Python 45%
JavaScript 29%
TypeScript 19%
Updated 1 weeks ago
Python
57%
Other

Sentry

Analytics · Developer Tools · Monitoring

45,632

Developer-first error tracking and performance monitoring platform with AI-powered root-cause analysis across 20+ languages and frameworks.

View details
95
Repo Health
80
Technical
66
Dependency
Built with
Python 57%
TypeScript 42%
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