asyncpg

A fast PostgreSQL database client library for Python built directly on asyncio, implementing the PostgreSQL binary wire protocol natively for high-throughput async applications.

Library
PyPI
v0.32.0
8,112 stars
Apache License 2.0

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
85 /100 Excellent
Development Activity 88
Maintenance 80
Community 72
Maturity 60
Momentum 40

Technical Analysis

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

asyncpg is a database driver for PostgreSQL written specifically for Python’s asyncio, avoiding the generic DB-API abstraction layer that most Python database libraries use. Instead of wrapping a synchronous C library or speaking a text-based protocol, asyncpg implements PostgreSQL’s binary wire protocol directly in Cython, which lets it decode rows into native Python objects (or fast Record objects) with minimal overhead. The maintainers report roughly 5x throughput over psycopg3 on typical workloads.

Because it exposes PostgreSQL’s protocol features directly rather than hiding them behind a lowest-common-denominator API, asyncpg gives first-class support for prepared statements, server-side cursors with partial iteration, and automatic encoding/decoding of composite types, arrays, ranges, and custom types. It requires Python 3.9+, supports PostgreSQL 9.5 through 18, and needs no external dependencies for the base install (GSSAPI/SSPI auth is an optional extra).

asyncpg is widely used as the underlying PostgreSQL driver for higher-level tools such as SQLAlchemy’s async engine and Databases, making it a common dependency in async web frameworks (FastAPI, Starlette) that need real database concurrency rather than thread-pool-wrapped synchronous drivers.

What You Get

  • A connect() coroutine and a connection-pool API (create_pool) for managing PostgreSQL connections under asyncio
  • Prepared statement support with automatic caching, plus explicit prepare() for reuse across many queries
  • Server-side cursors with partial iteration, so large result sets can be streamed instead of buffered in memory
  • Automatic encoding/decoding of PostgreSQL composite types, arrays, ranges, and JSON/JSONB without manual serialization
  • Fast Record objects (implemented in C) returned from queries instead of generic dict-like rows
  • Transaction and savepoint management via async context managers
  • COPY protocol support for bulk data import/export directly from Python iterables or files

Common Use Cases

  • Backing async web frameworks like FastAPI or Starlette with real non-blocking PostgreSQL access instead of a thread-pool-wrapped sync driver
  • Powering the async engine of higher-level ORMs and query layers such as SQLAlchemy’s asyncio support or the databases library
  • Bulk-loading large datasets into PostgreSQL efficiently via native COPY support
  • Streaming large query results with server-side cursors in ETL or reporting pipelines without exhausting memory
  • High-throughput services (APIs, job queues, analytics ingestion) where PostgreSQL round-trip latency and driver overhead directly affect request throughput

Under The Hood

Architecture

asyncpg is organized as a thin, well-factored asyncio driver: connection.py defines the public Connection class and top-level connect() coroutine (asyncpg/connection.py:2083) that negotiates connection parameters (DSN, host/port, SSL, server settings) and hands off to the Cython-implemented protocol package for the actual wire-level exchange; pool.py layers a Pool/PoolConnectionHolder/PoolConnectionProxy design on top of Connection to provide bounded, reusable connection pools with the same query surface (execute, fetch, executemany at pool.py:577-613) proxied transparently to an acquired connection. cursor.py, prepared_stmt.py, and transaction.py each wrap one PostgreSQL protocol concept as its own class, keeping concerns separated: statement caching lives in connection.py’s _StatementCache, while type encoding/decoding is fully isolated in the protocol/codecs/ Cython modules (array.pyx, range.pyx, record.pyx, pgproto.pyx). This separation means the core abstraction that would be most costly to change is the binary protocol layer in protocol/coreproto.pyx — everything above it (Connection, Pool, Cursor) is built assuming synchronous-looking coroutines over that protocol.

Tech Stack

The library is Python 3.9+ with its performance-critical path written in Cython (protocol/*.pyx, pgproto/) compiled to a C extension via setuptools and a custom build_ext in setup.py, with Cython pinned to >=3.2.1,<4.0.0 at build time. It has zero runtime dependencies for the base install beyond async_timeout on Python <3.11; GSSAPI/SSPI Kerberos auth is gated behind an optional gssauth extra (gssapi/sspilib). There is no ORM or web-framework dependency — asyncpg is consumed by higher-level layers (SQLAlchemy’s asyncio engine, databases) rather than depending on them. Testing pulls in pytest, flake8 (with flake8-pyi for the .pyi stub files), mypy in strict mode, and uvloop for testing against an alternate event loop implementation.

Code Quality

The tests/ directory contains dozens of focused test modules (test_connect.py, test_pool.py, test_cursor.py, test_codecs.py, test_transaction.py, test_cancellation.py, test_adversity.py for failure injection, and more), run via pytest with --strict-markers and coverage tracking configured through tool.coverage in pyproject.toml, including Cython source coverage via the Cython.Coverage plugin. mypy runs in strict = true mode across the pure-Python modules (a subset of internal modules such as connection, pool, and exceptions are explicitly exempted via ignore_errors, suggesting the typed surface is intentionally the public API rather than every internal helper). flake8 enforces style, and CI (.github/workflows/tests.yml) runs the suite against real PostgreSQL instances across supported versions and platforms (Linux/macOS/Windows), which is notably more rigorous than driver libraries that only mock the database layer.

What Makes It Unique

Unlike DB-API-style drivers (psycopg and friends) that route through a generic cursor abstraction designed to work the same way across many databases, asyncpg commits fully to PostgreSQL’s own binary wire protocol and exposes PostgreSQL-specific capabilities — typed composite/array/range decoding, native COPY support, server-side cursor iteration — as first-class API rather than afterthoughts. Implementing the protocol parsing and row materialization in Cython rather than pure Python is the specific technical choice behind its reported ~5x throughput advantage over psycopg3, since it avoids both the DB-API abstraction tax and much of the interpreter overhead on the hot path of decoding rows.

Used by 26 apps in this directory

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
51%
AGPL 3.0

Glean

Bookmarks Archiving · Knowledge Management

872

Self-hosted RSS reader and personal knowledge management tool with MCP server integration for AI assistant connectivity.

View details
73
Repo Health
75
Technical
70
Dependency
Built with
TypeScript 51%
Python 45%
Updated 2 days 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 yesterday
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 yesterday
Python
66%
Other

Morphik

AI Development · Databases · Search

3,717

Morphik is an AI-native ingestion and retrieval engine that lets developers store, search, and reason over visually rich documents — scanned PDFs, manuals, slides, and video — without duct-taping together OCR, an embedding model, and a vector database.

View details
64
Repo Health
71
Technical
67
Dependency
Built with
Python 66%
TypeScript 23%
Updated 4 days ago
TypeScript
92%
Other

OpenHands

AI Code Assistants · AI Development

91,695

The self-hosted developer control center for running AI coding agents — locally, in Docker, on VMs, or across cloud backends — with automation workflows for GitHub, Slack, and more.

View details
91
Repo Health
82
Technical
67
Dependency
Built with
TypeScript 92%
Updated 2 days ago
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 2 days 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
Python
49%
Other

Arize Phoenix

Analytics · Devops · Monitoring

11,976

Open-source AI observability platform for tracing, evaluating, and debugging LLM applications with built-in intelligence and MCP support.

View details
92
Repo Health
88
Technical
67
Dependency
Built with
Python 49%
TypeScript 43%
Updated yesterday

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