Cette page est affichée en anglais. Une traduction française est en cours.
ConfigsJul 16, 2026·3 min de lecture

asyncpg — Fast Async PostgreSQL Driver for Python

High-performance asyncio PostgreSQL client written in Cython, delivering 3x the throughput of psycopg2 for async workloads.

Prêt pour agents

Installation avec revue préalable

Cet actif nécessite une revue. Le prompt copié demande un dry-run, affiche les écritures, puis continue seulement après confirmation.

Needs Confirmation · 66/100Policy : confirmer
Surface agent
Tout agent MCP/CLI
Type
Skill
Installation
Single
Confiance
Confiance : Established
Point d'entrée
asyncpg Overview
Commande avec revue préalable
npx -y tokrepo@latest install 03d79b4b-8136-11f1-9bc6-00163e2b0d79 --target codex

Dry-run d'abord, confirmez les écritures, puis lancez cette commande.

Introduction

asyncpg is a high-performance asyncio PostgreSQL client library for Python, written in Cython. It implements the PostgreSQL binary protocol directly, bypassing libpq, and achieves 3x the throughput of psycopg2 in async workloads. It is the recommended PostgreSQL driver for async Python frameworks like FastAPI, Starlette, and Sanic.

What asyncpg Does

  • Connects to PostgreSQL using native asyncio with the binary wire protocol
  • Executes queries with automatic type conversion between Python and PostgreSQL types
  • Manages connection pools with configurable min/max size and health checks
  • Supports prepared statements, transactions, cursors, and COPY operations
  • Handles PostgreSQL LISTEN/NOTIFY for real-time event subscriptions

Architecture Overview

asyncpg communicates directly with PostgreSQL using the binary protocol (not libpq), which eliminates the overhead of text serialization. Cython-compiled codecs handle type conversion at near-C speed. The connection pool maintains a set of warmed connections with prepared statement caches, reducing per-query overhead. Each connection runs its own protocol state machine integrated with the asyncio event loop.

Setup & Configuration

  • Install via pip install asyncpg (requires a C compiler for Cython extensions)
  • Connect with asyncpg.connect(dsn) or use asyncpg.create_pool(dsn) for pooling
  • Configure pool size with min_size and max_size parameters
  • Set statement_cache_size to control per-connection prepared statement caching
  • Use server_settings dict to set PostgreSQL session parameters like search_path

Key Features

  • Binary protocol implementation delivers 3x throughput vs text-based drivers
  • Cython-compiled codecs for fast Python-to-PostgreSQL type conversion
  • Built-in connection pooling with health checks and automatic reconnection
  • Prepared statement caching reduces repeated query parsing overhead
  • LISTEN/NOTIFY support for real-time PostgreSQL event streaming

Comparison with Similar Tools

  • psycopg (v3) — supports both sync and async, asyncpg is async-only but faster for pure async workloads
  • psycopg2 — synchronous only, asyncpg is async-native with higher throughput
  • SQLAlchemy asyncio — ORM layer that can use asyncpg as its backend driver
  • databases — async database abstraction, often uses asyncpg under the hood for PostgreSQL

FAQ

Q: Can I use asyncpg with SQLAlchemy? A: Yes. SQLAlchemy 1.4+ supports asyncpg as a dialect via postgresql+asyncpg:// connection strings.

Q: How does asyncpg compare to psycopg3 in performance? A: asyncpg is generally faster for raw query throughput due to its Cython implementation and binary protocol. psycopg3 offers broader compatibility and a sync API.

Q: Does asyncpg support connection pooling? A: Yes. Use asyncpg.create_pool() for a built-in pool with configurable min/max connections and automatic health checks.

Q: Can I use asyncpg with Django? A: Django does not natively support asyncpg. Use it with async frameworks like FastAPI, Starlette, or Sanic instead.

Sources

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires