Database Drivers

Libraries for connecting and operating databases.

Each database calls for a Python database driver built for it. Reach PostgreSQL through Psycopg, and MySQL through mysqlclient. sqlite3 comes with Python.

How to choose:

  • PostgreSQL, from sync or async code: Psycopg
  • MySQL or MariaDB, Django projects included: mysqlclient
  • SQLite with nothing to install: sqlite3
  • MySQL in pure Python: PyMySQL
  • An asyncio-only PostgreSQL service: asyncpg
  • Loading JSON or CSV into SQLite, from Python or the shell: sqlite-utils
  • ClickHouse, even behind proxies and load balancers: clickhouse-connect
  • ClickHouse over its native TCP protocol: clickhouse-driver
  • Any database with an ODBC driver: pyodbc
  • Oracle Database: python-oracledb
  • SQL Server or Azure SQL, with no driver manager to set up: mssql-python
  • Redis, MongoDB, or Apache Cassandra: redis-py, PyMongo, or cassandra-driver

Listed in editorial order, grouped by use case. Click a column to re-sort the whole list.

Press / to search. Tap a tag to filter. Click any row for details.

Search and filter

Results

Row number Tags

MySQL 2 projects

A pure-Python MySQL and MariaDB client library, based on PEP 249.
PyMySQL/github.com/PyMySQL/PyMySQL / /72,866,806 downloads/month
MySQL and MariaDB connector (MySQLdb1 fork).
PyMySQL/github.com/PyMySQL/mysqlclient / /15,846,846 downloads/month

PostgreSQL 2 projects

A PostgreSQL adapter for Python, the successor to psycopg2.
psycopg/github.com/psycopg/psycopg / /127,387,768 downloads/month
A fast PostgreSQL Database Client Library for Python/asyncio.
MagicStack/github.com/MagicStack/asyncpg / /103,502,581 downloads/month

SQLite 2 projects

(Python standard library) SQLite interface compliant with DB-API 2.0.
Python CLI utility and library for manipulating SQLite databases.
simonw/github.com/simonw/sqlite-utils / /878,452 downloads/month

ClickHouse 2 projects

The official ClickHouse client, with SQLAlchemy and Superset connectors.
ClickHouse/github.com/ClickHouse/clickhouse-connect / /26,734,617 downloads/month
Python driver with native interface for ClickHouse.
mymarilyn/github.com/mymarilyn/clickhouse-driver / /10,601,068 downloads/month

Other Relational Databases 3 projects

An ODBC bridge for connecting to SQL Server and any other ODBC-accessible database.
mkleehammer/github.com/mkleehammer/pyodbc / /26,684,919 downloads/month
The official Python driver for Oracle Database, successor to cx_Oracle.
oracle/github.com/oracle/python-oracledb / /17,636,506 downloads/month
Official Microsoft driver for SQL Server and Azure SQL, built on ODBC for high performance.
microsoft/github.com/microsoft/mssql-python / /2,718,798 downloads/month

NoSQL Databases 3 projects

The Python client for Redis.
redis/github.com/redis/redis-py / /195,454,499 downloads/month
The official Python client for MongoDB.
mongodb/github.com/mongodb/mongo-python-driver / /63,817,002 downloads/month
The Python Driver for Apache Cassandra.
apache/github.com/apache/cassandra-python-driver / /3,629,165 downloads/month

Database Drivers guide

Psycopg is the PostgreSQL driver Django recommends. One design serves sync and async code: AsyncConnection and AsyncCursor mirror the sync classes, so you mostly add await. Open each connection in a with block. It commits when the block ends, rolls back on an exception, and closes the connection either way.

asyncpg is built for asyncio only, and skips DB-API on purpose to fit PostgreSQL better. In a server, create a pool with asyncpg.create_pool() at startup, then take a connection per request with async with pool.acquire().

Django calls mysqlclient the recommended choice for MySQL, and ships its own adapter for it. It's a native driver built against MySQL's client library. Where you can't install that library and its headers, use PyMySQL, which SQLAlchemy calls a pure Python port of MySQLdb that targets full compatibility. The licenses differ too: mysqlclient is GPL, and PyMySQL is MIT.

sqlite3 comes with Python, and SQLite needs no server. Use the connection as a context manager: it commits when the block succeeds and rolls back when it raises.

sqlite-utils sits on top of sqlite3 to create and fill databases, not to be a full ORM. Pass insert_all() a list of dicts, and it creates the table and columns for you. Its command-line tool pipes JSON or CSV straight into a new database file.

clickhouse-connect is ClickHouse's official client and talks HTTP, so it works through load balancers, proxies, and enterprise network controls. clickhouse-driver uses the native TCP protocol instead, which its docs say is more configurable and sends compact binary data.

pyodbc reaches any database through that database's ODBC driver, plus one driver manager for all of them. Windows has one built in; on macOS and Unix, you install one first. For SQL Server and Azure SQL, Microsoft's own mssql-python connects without an external driver manager.

python-oracledb is Oracle's official driver. In its default Thin mode, it connects straight to Oracle Database without Oracle Client libraries.

redis-py is the Python client for Redis, and MongoDB's docs call PyMongo the recommended way to work with MongoDB from Python. cassandra-driver talks to Apache Cassandra over Cassandra's binary protocol, with sync and async APIs.

With any SQL database, pass values as query parameters instead of formatting them into the SQL string. The sqlite3 docs say this avoids SQL injection. For more per database, see awesome-mysql, awesome-postgres, and awesome-sqlite.

Know a project that belongs here?

Tell us what it does and why it stands out.