The `databases` library provides asynchronous database support for Python, designed to work with `asyncio` and `await`. It supports PostgreSQL, MySQL, and SQLite, and integrates well with SQLAlchemy Core expression language. The current version is 0.9.0, and it has an active development cadence with regular updates.
pip install databasesVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates connecting to a database, executing a DDL statement to create a table, inserting a record, and fetching all records. It uses an in-memory SQLite database by default but can be configured with an environment variable for other databases. Remember to install the appropriate database driver (e.g., `pip install databases[sqlite]`).
Ensure your project runs on Python 3.8 or higher. If using SQLAlchemy, consider upgrading to SQLAlchemy 2.x. If you must use older SQLAlchemy 1.x, consult `databases` documentation for specific version compatibility.
Carefully review concurrent database operations in your application. Ensure that connections and transactions are explicitly managed or passed between tasks where shared behavior is intended, rather than relying on implicit inheritance.
Always install `databases` with the appropriate extras, e.g., `pip install databases[postgresql]`, or install the driver manually.
Always check the specific release notes for `databases` regarding SQLAlchemy compatibility. For `>=0.9.0`, it's highly recommended to use SQLAlchemy 2.x. If sticking with SQLAlchemy 1.x, careful version pinning of both `databases` and `SQLAlchemy` is required.
Install the library using pip: `pip install databases`. If a specific database backend is used, ensure its corresponding async driver is also installed, e.g., `pip install databases[postgresql]` (which installs `asyncpg`) or `pip install databases[mysql]` (which installs `aiomysql`) or `pip install databases[sqlite]` (which installs `aiosqlite`).
Ensure `await database.connect()` is called and awaited before any database interactions. For robust connection management, use an `async with` block: `async with database: await database.fetch_one(...)`.
Verify that the database server is running, the connection URL (host, port, database name, user, password) is absolutely correct, and network configurations (like firewalls) permit the connection.
Install the required driver. For PostgreSQL, use `pip install asyncpg`. For MySQL, `pip install aiomysql`. For SQLite, `pip install aiosqlite`. Using `pip install databases[backend_name]` is the recommended way to ensure correct driver installation.