text: Async database support for Python.
  Project description
  Databases
  Databases gives you simple asyncio support for a range of databases.
  It allows you to make queries using the powerful SQLAlchemy Core expression language, and provides support for PostgreSQL, MySQL, and SQLite.
  Databases is suitable for integrating against any async Web framework, such as Starlette, Sanic, Responder, Quart, aiohttp, FastAPI, or Bocadillo.
  Requirements: Python 3.6+
  Installation
  $ pip install databases
  You can install the required database drivers with:
  $ pip install databases[postgresql] $ pip install databases[mysql] $ pip install databases[sqlite]
  Driver support is providing using one of asyncpg, aiomysql, or aiosqlite.): ... # Close all connection in the connection pool await database.disconnect()
  Connections are managed as task-local state, with driver implementations transparently using connection pooling behind the scenes.()
  If you're integrating against a web framework, then you'll probably want to hook into framework startup or shutdown events. For example, with Starlette you would use the following:
  @app.on_event("startup") async def startup(): await database.connect() @app.on_event("shutdown") async def shutdown(): await database.disconnect()
  Connection options
  The PostgreSQL and MySQL backends provide a few connection options for SSL and for configuring the connection pool.
  # Use an SSL connection. database = Database('postgresql://localhost/example?ssl=true') # Use a connection pool of between 5-20 connections. database = Database('mysql://localhost/example?min_size=5&max_size=20') TESTING: database = Database(TEST_DATABASE_URL, force_rollback=True) else: database = Database(DATABASE_URL)): ...
  Migrations
  Because
  databases uses SQLAlchemy core, you can integrate with Alembic
  for database migration support.
  $ pip install alembic $ alembic init migrations
  You'll want to set things up so that Alembic references the configured
  DATABASE_URL, and uses your table metadata.
  In
  alembic.ini remove the following line:
  sqlalchemy.url = driver://user:pass@localhost/dbname
  In
  migrations/env.py, you need to set the
  'sqlalchemy.url' configuration key,
  and the
  target_metadata variable. You'll want something like this:
  # The Alembic Config object. config = context.config # Configure Alembic to use our DATABASE_URL and our table definitions. # These are just examples - the exact setup will depend on whatever # framework you're integrating against. from myapp.settings import DATABASE_URL from myapp.tables import metadata config.set_main_option('sqlalchemy.url', str(DATABASE_URL)) target_metadata = metadata ...
  Note that migrations will use a standard synchronous database driver,
  rather than using the async drivers that
  databases provides support for.
  This will also be the case if you're using SQLAlchemy's standard tooling, such
  as using
  metadata.create_all(engine) to setup the database tables.
  Note for MySQL:
  For MySQL you'll probably need to explicitly specify the
  pymysql dialect when
  using Alembic since the default MySQL dialect does not support Python 3.
  If you're using the
  databases.DatabaseURL datatype, you can obtain this using
  DATABASE_URL.replace(dialect="pymysql")
  — ⭐️ —
  Databases is BSD licensed code. Designed & built in Brighton, England.
  Project details
  Release history Release notifications
  Download files
  Download the file for your platform. If you're not sure which to choose, learn more about installing packages.