psycopg2

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

Library
PyPI
v2.9.12
3,654stars
LGPL-3.0

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
48/100Fair
Development Activity12
Maintenance0
Community80
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
66/100Good
Architecture75
Code Quality80
Innovation55
Learning Curve55

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

Apache Airflow

Data Engineering

46,755

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
Python90%
Updated yesterday
Python
59%
Apache 2.0

argilla

AI Development · Data Engineering

5,098

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
Python59%
Jupyter Notebook21%
Updated 1 weeks ago
Python
67%
Other

AutoGPT

Automation · Productivity · AI Assistants

187,177

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
Python67%
TypeScript32%
Updated today
Python
68%
Other

Baserow

No Code Platforms · Databases

5,830

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
Python68%
JavaScript16%
Vue12%
Updated yesterday
C++
68%
Apache 2.0

ClickHouse

Databases · Analytics · Data Engineering

49,673

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
68
Dependency
Built with
C++68%
Python13%
Updated today
TypeScript
84%
Apache 2.0

Continue

Developer Tools · AI Development · AI Code Assistants

35,808

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

View details
81
Repo Health
88
Technical
62
Dependency
Built with
TypeScript84%
Updated yesterday
Python
44%
MIT

/dev/push

Developer Tools · Devops

4,745

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
43
Repo Health
68
Technical
72
Dependency
Built with
Python44%
HTML31%
CSS17%
Updated 6 months ago
TypeScript
50%
Other

Dify

No Code Platforms · AI Development · Developer Tools

154,649

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

View details
92
Repo Health
85
Technical
67
Dependency
Built with
TypeScript50%
Python46%
Updated yesterday
Python
63%
BSD 3

Flagsmith

Developer Tools · Devops · Ab Testing Experimentation

6,539

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
Python63%
TypeScript31%
Updated 2 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

Search