API Reference

asyncpg

Fast PostgreSQL client for Python asyncio with native binary protocol, prepared statements, and pooling.

Official Documentation ↗
CompatibilityThis documentation covers version v0.30. Check the official docs for the latest API surface.

Installation

pippip install asyncpg

Methods

Fast PostgreSQL client for Python asyncio with native binary protocol, prepared statements, and pooling.

Overview

asyncpg talks directly to PostgreSQL over its binary protocol, skipping text encoding layers. Parameters are sent typed, results arrive as lightweight Record objects, and queries are automatically prepared and cached per connection.

The pool hands out leased connections, transactions are context managers, and LISTEN/NOTIFY plus COPY operations are first-class. It is consistently among the fastest PostgreSQL drivers for Python, which is why ORMs adopt it as their async engine.

Core Methods

The most frequently used entry points, with signatures and runnable examples:

  • asyncpg.connect — Opens a single connection speaking the PostgreSQL binary protocol with automatic parameter encoding.
  • conn.fetch / fetchrow / fetchval — Runs queries with positional $1 placeholders; Records behave like mappings keyed by column name.
  • conn.execute — Runs statements that modify data or schema and returns the PostgreSQL status string such as DELETE 3.
  • asyncpg.create_pool — Creates a connection pool; acquire leases a connection for the duration of a context block.
  • conn.transaction — Runs a block inside a transaction, committing on success and rolling back on exception.

Usage Notes

  • Standardize on one pool per process, sized for your database, and acquire connections only for the duration of a unit of work.
  • Keep query strings constant and vary parameters so the prepared-statement cache stays effective.
  • Return connections via context managers; a leaked acquire starves the pool under load.

Common Pitfalls

  • Interpolating values with f-strings reintroduces SQL injection and breaks statement caching.
  • Record objects die with the connection; convert to dicts for data that outlives the request.
  • Exceeding max_size queues waiters without error; long transactions hold leases and stall the pool.

FAQ

Why $1 instead of %s or ? placeholders?
asyncpg uses the native PostgreSQL numbered placeholders because it prepares statements server-side. Positional Python arguments map to $1, $2 in order, and the server sees typed parameters, not string interpolation.
How does asyncpg compare to SQLAlchemy?
asyncpg is a driver: hand-written SQL, manual pool management, no ORM. SQLAlchemy (with its asyncpg dialect) adds models, query building, and migrations. Many services use both, SQLAlchemy for schema work and raw asyncpg for hot paths.
When do I see cached statements help?
Every unique query string is prepared once per connection and cached (statement_cache_size). Repeating identical SQL with different parameters is fast; generating dynamic string variations defeats the cache and risks plan pollution.