Databases

Simple asyncio support for PostgreSQL, MySQL, and SQLite, with queries built using SQLAlchemy Core.

Library
PyPI
v0.9.0
3,996stars
BSD 3-Clause License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
45/100Fair
Development Activity0
Maintenance20
Community60
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
82/100Excellent
Architecture84
Code Quality82
Innovation80
Learning Curve80

Databases is an async database library for Python that gives you a single asyncio interface over PostgreSQL, MySQL, and SQLite. It lets you compose queries with the SQLAlchemy Core expression language and execute them against high-performance async drivers such as asyncpg, aiomysql, and aiosqlite.

Built by the Encode team behind Starlette and HTTPX, Databases is designed to slot into any async web framework — FastAPI, Starlette, Sanic, Quart, aiohttp, and others — providing connection pooling, transactions, and per-connection context management without pulling in a full ORM.

What You Get

  • A unified async API over PostgreSQL, MySQL, and SQLite backends
  • Query construction with the SQLAlchemy Core expression language or raw SQL
  • Connection pooling and explicit connect/disconnect lifecycle management
  • Transaction support via async context managers and decorators
  • Pluggable driver backends (asyncpg, aiopg, aiomysql, asyncmy, aiosqlite)

Common Use Cases

  • Running non-blocking database queries inside FastAPI or Starlette apps
  • Executing SQLAlchemy Core queries against an async Postgres or MySQL backend
  • Wrapping multi-statement operations in async transactions with rollback
  • Adding async SQLite access to scripts and lightweight services

Under The Hood

Architecture - The core Database class in databases/core.py parses a database URL, selects a backend from databases/backends, and manages a connection pool. Per-task connection state is tracked with ContextVar and weakref, so each asyncio task gets an isolated Connection with its own transaction stack. Queries expressed as SQLAlchemy ClauseElements are compiled and handed to the backend, which wraps a native async driver (asyncpg, aiomysql, aiosqlite, etc.) behind the shared DatabaseBackend/TransactionBackend interfaces defined in interfaces.py.

Tech Stack - Pure Python (3.8+) built on SQLAlchemy Core for query compilation, with optional async driver dependencies installed via extras. It ships py.typed for type checkers and uses contextvars for task-local connection scoping.

Code Quality - The codebase is compact and cleanly separated into core, interfaces, importer, and per-dialect backends. It is fully type-annotated, includes an extensive CI test suite exercising each backend, and uses interface abstractions to keep driver-specific code isolated.

API Design - The public surface is small and intuitive: Database(url), connect()/disconnect(), fetch_all/fetch_one/execute, and an async with database.transaction() construct. Reusing SQLAlchemy Core means developers already familiar with it need almost no new learning, and integration into async frameworks is a few lines.

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

Search