Direct database drivers · asyncpg (async Postgres) + PyMySQL (sync MySQL) · verified against asyncpg 0.31 / PyMySQL 1.2 (2026)

Python DB Drivers cheat sheet

Two popular low-level database drivers, side by side. asyncpg is a fast, async-only PostgreSQL driver built on asyncio that speaks the PG binary protocol directly (no DB-API) — it uses numbered $1, $2 placeholders. PyMySQL is a pure-Python, synchronous MySQL/MariaDB client that follows the standard DB-API 2.0 connectcursorexecute flow with %s placeholders. This sheet covers both, highlights their differences, and points to the wider driver landscape. Targets asyncpg 0.31.x & PyMySQL 1.2.x, Python 3.9+.

asyncpg — connect & query asyncpg — pools & power PyMySQL — connect & query PyMySQL — cursors & txns compare & ecosystem gotcha most common

Verified 2026-08-31 against magicstack.github.io/asyncpg & pymysql.readthedocs.io / PyPI (asyncpg 0.31.0, 2025-11-24, PostgreSQL 9.5–18; PyMySQL 1.2.0, 2026-05-19). Key difference: asyncpg placeholders are $1, $2…; PyMySQL (DB-API) uses %s. Both: always parameterize — never format values into SQL.

Outline

asyncpg for async PostgreSQL ($1 params, connection pools); PyMySQL for sync MySQL (DB-API, %s params, remember to commit). The last section compares them and maps the landscape.

asyncpg (PostgreSQL, async)

  1. 1Install & connect
  2. 2Query & params
  3. 3Connection pools
  4. 4Transactions, COPY & types

PyMySQL (MySQL, sync)

  1. 5Install & connect
  2. 6Cursor & execute
  3. 7Fetch & cursors
  4. 8Transactions & bulk

Compare & ecosystem

  1. 9asyncpg vs PyMySQL
  2. 10Driver landscape

asyncpg — PostgreSQL, async

Fast async Postgres: connect, query with $1 params, and pool connections.

1Install & connectasyncio
2Query & params$1, $2 …
3Connection poolsthe prod pattern
4Transactions, COPY & typespower features

PyMySQL — MySQL, sync

Standard DB-API: connect → cursor → execute (%s params) → commit.

5Install & connectDB-API 2.0
6Cursor & execute%s params
7Fetch & cursorsreading rows
8Transactions & bulkcommit!

Compare & Ecosystem

The differences that trip people up, and how these fit the wider driver world.

9asyncpg vs PyMySQLside by side
10Driver landscapewhat to pick

Worth memorizing

asyncpg = async PG$1,$2 params · not DB-API
PyMySQL = sync MySQL%s params · DB-API 2.0
await conn.fetch/fetchrow/fetchvalasyncpg reads (Record objs)
await conn.execute($1)asyncpg write/statement
create_pool + pool.acquire()asyncpg prod pattern
copy_records_to_tableasyncpg fastest bulk load
cursor.execute("%s", (v,))PyMySQL — values in a tuple
DictCursorPyMySQL rows as dicts
conn.commit()PyMySQL — REQUIRED, autocommit off
SSCursorPyMySQL server-side streaming
never format SQLalways parameterize — injection risk
most apps -> SQLAlchemypooling + ORM over the driver