Integrate Spanner with SQLAlchemy ORM (GoogleSQL dialect)

SQLAlchemy is a Python SQL toolkit and ObjectRelational Mapper.

TheSpanner dialect for SQLAlchemy letsSQLAlchemy users to use Spanner databases. The dialect is built on topof theSpanner API, which is designed inaccordance withPEP-249, and is compatible withSQLAlchemy versions between1.1.13 and1.3.23, and2.0.

Set up the Spanner dialect for SQLAlchemy

To set up the Spanner dialect for SQLAlchemy in your application, install thesqlalchemy-spanner package.

pip3installsqlalchemy-spanner

Alternatively, you can install from source.

gitclonehttps://github.com/googleapis/python-spanner-sqlalchemy.gitcdpython-spanner-sqlalchemypythonsetup.pyinstall

As authentication for the Spanner dialect for SQLAlchemy, provide theservice accountJSON credentials file location in theGOOGLE_APPLICATION_CREDENTIALS environment variable. Otherwise, the dialectcan also use the default credentials set in the gcloud CLIapplication.

Use the Spanner dialect for SQLAlchemy

For more information about the available features, limitations of the dialect,recommendations on how to use the dialect, and for code samples, please consultthereference documentation onGitHub.

What's next

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026-02-19 UTC.