Integrate Spanner with SQLAlchemy ORM (GoogleSQL dialect)

SQLAlchemy is a Python SQL toolkit and Object Relational Mapper.

The Spanner dialect for SQLAlchemy lets SQLAlchemy users to use Spanner databases. The dialect is built on top of the Spanner API, which is designed in accordance with PEP-249, and is compatible with SQLAlchemy versions between 1.1.13 and 1.3.23, and 2.0.

Set up the Spanner dialect for SQLAlchemy

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

pip3installsqlalchemy-spanner

Alternatively, you can install from source.

gitclonehttps://github.com/googleapis/python-spanner-sqlalchemy.git
cdpython-spanner-sqlalchemy
pythonsetup.pyinstall

As authentication for the Spanner dialect for SQLAlchemy, provide the service account JSON credentials file location in the GOOGLE_APPLICATION_CREDENTIALS environment variable. Otherwise, the dialect can also use the default credentials set in the gcloud CLI application.

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 consult the reference documentation on GitHub.

What's next

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

Last updated 2025年11月24日 UTC.