Skip to main content

Welcome!

prefect-sqlalchemy helps you connect to a database in your Prefect flows.

Getting started

Install prefect-sqlalchemy

The following command will install a version of prefect-sqlalchemy compatible with your installed version of prefect. If you don’t already have prefect installed, it will install the newest version of prefect as well.
Upgrade to the latest versions of prefect and prefect-sqlalchemy:

Register newly installed block types

Register the block types in the prefect-sqlalchemy module to make them available for use.

Examples

Save credentials to a block

To use the load method on Blocks, you must have a block saved through code or saved through the UI.
Load the saved block that holds your credentials:
The required arguments depend upon the desired driver. For example, SQLite requires only the driver and database arguments:

Work with databases in a flow

To set up a table, use the execute and execute_many methods. Use the fetch_many method to retrieve data in a stream until there’s no more data. Use the SqlAlchemyConnector as a context manager, to ensure that the SQLAlchemy engine and any connected resources are closed properly after you’re done with them.
Async supportFor async workflows with async database drivers (like AsyncDriver.SQLITE_AIOSQLITE or AsyncDriver.POSTGRESQL_ASYNCPG), use AsyncSqlAlchemyConnector instead of SqlAlchemyConnector. See the Async tab below for a complete example.

Resources

Refer to the prefect-sqlalchemy SDK documentation to explore all the capabilities of the prefect-sqlalchemy library. For assistance using SQLAlchemy, consult the SQLAlchemy documentation.