asyncpg

0.31.0 · active · verified Tue Mar 24

High-performance async PostgreSQL driver for Python/asyncio. Implements PostgreSQL binary protocol natively — ~5x faster than psycopg3 in benchmarks. Current version: 0.31.0 (Nov 2025). Still pre-1.0. NOT DB-API 2.0 compliant — uses $1/$2 placeholders not %s. No dict row support out of the box — returns Record objects. Major footgun: prepared statements break with pgbouncer in transaction/statement mode (Supabase, Neon poolers).

Warnings

Install

Imports

Quickstart

asyncpg connection pool with correct $1/$2 placeholders.

# pip install asyncpg
import asyncpg
import asyncio

async def main():
    # Connection pool for production
    pool = await asyncpg.create_pool(
        'postgresql://user:pass@localhost/mydb',
        min_size=2,
        max_size=10
    )

    async with pool.acquire() as conn:
        # $1, $2 — not %s
        await conn.execute(
            'INSERT INTO users(name, email) VALUES($1, $2)',
            'Alice', 'alice@example.com'
        )

        # fetchrow returns asyncpg.Record — dict-like
        row = await conn.fetchrow(
            'SELECT * FROM users WHERE name = $1', 'Alice'
        )
        print(row['name'])   # 'Alice'
        print(dict(row))     # convert to plain dict

        # fetch returns list of Records
        rows = await conn.fetch('SELECT id, name FROM users')

    await pool.close()

asyncio.run(main())

view raw JSON →