psycopg2

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

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

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
72 /100 Good
Development Activity 76
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
89%
Apache 2.0

Apache Airflow

Data Engineering

46,995

Define, schedule, and monitor complex data workflows as Python code — with a powerful UI, 80+ provider integrations, and battle-tested scalability across thousands of production deployments.

View details
96
Repo Health
89
Technical
64
Dependency
Built with
Python 89%
Updated 5 days ago
Python
59%
Apache 2.0

argilla

AI Development · Data Engineering

5,125

Collaborate on high-quality AI training data with a self-hosted annotation platform built for LLMs, NLP, and multimodal models.

View details
65
Repo Health
81
Technical
61
Dependency
Built with
Python 59%
Jupyter Notebook 21%
Updated 1 weeks ago
Python
66%
Other

AutoGPT

AI Assistants · Automation · Productivity

187,596

Build, deploy, and run autonomous AI agents that automate complex multi-step workflows using a visual block-based graph editor.

View details
93
Repo Health
78
Technical
66
Dependency
Built with
Python 66%
TypeScript 33%
Updated 5 days ago
Python
68%
Other

Baserow

Databases · No Code Platforms

6,012

Open-source no-code platform to build databases, apps, automations, and AI agents — self-hosted or cloud, with full data ownership.

View details
89
Repo Health
84
Technical
68
Dependency
Built with
Python 68%
JavaScript 16%
Vue 11%
Updated 6 days ago
C++
68%
Apache 2.0

ClickHouse

Analytics · Data Engineering · Databases

50,116

Open-source column-oriented database that delivers real-time analytical queries on petabyte-scale data with millisecond latency.

View details
95
Repo Health
90
Technical
64
Dependency
Built with
C++ 68%
Python 14%
Updated 5 days ago
TypeScript
84%
Apache 2.0

Continue

AI Code Assistants · AI Development · Automation

36,049

Open-source coding agent for VS Code, JetBrains, and CLI with support for 30+ LLM providers.

View details
73
Repo Health
88
Technical
62
Dependency
Built with
TypeScript 84%
Updated 6 days ago
Python
44%
MIT

/dev/push

Developer Tools · Devops

4,757

Self-hosted, open-source Vercel alternative that deploys Python, Node.js, PHP, and any Docker-compatible app from a Git push, with zero-downtime rollouts and real-time logs.

View details
44
Repo Health
68
Technical
73
Dependency
Built with
Python 44%
HTML 31%
CSS 17%
Updated 7 months ago
TypeScript
50%
Other

Dify

AI Development · Design Tools · Developer Tools

157,364

Visual LLM workflow platform with RAG pipelines, agent capabilities, and model management for building production AI applications.

View details
92
Repo Health
85
Technical
66
Dependency
Built with
TypeScript 50%
Python 47%
Updated 5 days ago
Python
63%
BSD 3

Flagsmith

Ab Testing Experimentation · Developer Tools · Devops

6,577

Open-source feature flagging, remote config, and A/B/multivariate testing platform for web, mobile, and server-side apps — self-host or use the hosted SaaS.

View details
90
Repo Health
82
Technical
63
Dependency
Built with
Python 63%
TypeScript 31%
Updated 1 weeks 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