---
title: Use SQLAlchemy with Databricks
description: Learn how to use the SQLAlchemy dialect for Databricks, included with the Databricks SQL Connector for Python, to use SQLAlchemy to read and write Databricks SQL on Databricks compute resources.
canonical_url: https://docs.databricks.com/aws/en/dev-tools/sqlalchemy
---

# Use SQLAlchemy with Databricks

Databricks provides a [SQLAlchemy](https://www.sqlalchemy.org/) *dialect* (the system SQLAlchemy uses to communicate with various types of database API implementations and databases) for Databricks. SQLAlchemy is a Python SQL toolkit and Object Relational Mapper (ORM). SQLAlchemy provides a suite of well known enterprise-level persistence patterns, designed for efficient and high-performing database access, adapted into a simple and Pythonic domain language. See [Features and Philosophy](https://www.sqlalchemy.org/features.html).

The SQLAlchemy dialect for Databricks needs to be installed to use the SQLAlchemy features with Databricks. This article covers SQLAlchemy dialect for Databricks version 1.0 and 2.0, which will be based on Databricks SQL Connector for Python version 4.0.0 or above.

## Requirements

* A development machine running Python >=3.8 and \<=3.11.
* Databricks recommends that you use Python virtual environments, such as those provided by [venv](https://docs.python.org/3/library/venv.html) that are included with Python. Virtual environments help to ensure that you are using the correct versions of Python and the Databricks SQL Connector for Python together. Setting up and using virtual environments is outside of the scope of this article. For more information, see [Creating virtual environments](https://packaging.python.org/en/latest/tutorials/installing-packages/#creating-and-using-virtual-environments).
* An existing [cluster](https://docs.databricks.com/aws/en/compute/index.md) or [SQL warehouse](https://docs.databricks.com/aws/en/compute/sql-warehouse/index.md).

## Get started

* Install databricks-sqlalchemy for SQLAlchemy v1 using `pip install databricks-sqlalchemy~=1.0` or for SQLAlchemy v2 using `pip install databricks-sqlalchemy`. For version information, see the [databricks-sqlalchemy release history](https://pypi.org/project/databricks-sqlalchemy/#history).
* Gather the following information for the cluster or SQL warehouse that you want to use:

  ### Cluster

  * The server hostname of the cluster. You can get this from the **Server Hostname** value in the **[Advanced Options > JDBC/ODBC](https://docs.databricks.com/aws/en/integrations/compute-details.md)** tab for your cluster.
  * The HTTP path of the cluster. You can get this from the **HTTP Path** value in the **[Advanced Options > JDBC/ODBC](https://docs.databricks.com/aws/en/integrations/compute-details.md)** tab for your cluster.

  ### SQL warehouse

  * The server hostname of the SQL warehouse. You can get this from the **Server Hostname** value in the [Connection Details](https://docs.databricks.com/aws/en/integrations/compute-details.md) tab for your SQL warehouse.
  * The HTTP path of the SQL warehouse. You can get this from the **HTTP Path** value in the [Connection Details](https://docs.databricks.com/aws/en/integrations/compute-details.md) tab for your SQL warehouse.

## Authentication

The SQLAlchemy dialect for Databricks uses the Databricks SQL Connector for Python to connect, and passes the values in `connect_args` to the connector. It supports the following authentication types:

* [Personal access token authentication](https://docs.databricks.com/aws/en/dev-tools/sqlalchemy.md#pat)
* [OAuth user-to-machine (U2M) authentication](https://docs.databricks.com/aws/en/dev-tools/sqlalchemy.md#oauth-u2m)
* [OAuth machine-to-machine (M2M) authentication](https://docs.databricks.com/aws/en/dev-tools/sqlalchemy.md#oauth-m2m)

### <a id="pat" />Personal access token authentication

To create a Databricks personal access token, follow the steps in [Create personal access tokens for workspace users](https://docs.databricks.com/aws/en/dev-tools/auth/pat.md#pat-user).

To authenticate the SQLAlchemy dialect, use the following code snippet. This snippet assumes that you have set the following environment variables:

* `DATABRICKS_TOKEN`, set to the Databricks personal access token.
* `DATABRICKS_SERVER_HOSTNAME`set to the **Server Hostname** value for your cluster or SQL warehouse.
* `DATABRICKS_HTTP_PATH`, set to **HTTP Path** value for your cluster or SQL warehouse.
* `DATABRICKS_CATALOG`, set to the target catalog in Unity Catalog.
* `DATABRICKS_SCHEMA`, set to the target schema (also known as a database) in Unity Catalog.

To set environment variables, see your operating system's documentation.

```python
import os
from sqlalchemy import create_engine

access_token    = os.getenv("DATABRICKS_TOKEN")
server_hostname = os.getenv("DATABRICKS_SERVER_HOSTNAME")
http_path       = os.getenv("DATABRICKS_HTTP_PATH")
catalog         = os.getenv("DATABRICKS_CATALOG")
schema          = os.getenv("DATABRICKS_SCHEMA")

engine = create_engine(
  url = f"databricks://token:{access_token}@{server_hostname}?" +
        f"http_path={http_path}&catalog={catalog}&schema={schema}"
)

# ...
```

You use the preceding `engine` variable to connect to your specified catalog and schema through your Databricks compute resource.

### <a id="oauth-u2m" />OAuth user-to-machine (U2M) authentication

[OAuth U2M authentication](https://docs.databricks.com/aws/en/dev-tools/auth/oauth-u2m.md) uses real-time human sign-in and consent to authenticate the target Databricks user account. For the Databricks SQL Connector for Python versions that support it on each cloud, see [OAuth user-to-machine (U2M) authentication](https://docs.databricks.com/aws/en/dev-tools/python-sql-connector.md#auth-u2m).

To authenticate the SQLAlchemy dialect with OAuth U2M, use the following code snippet. This snippet assumes that you have set the `DATABRICKS_SERVER_HOSTNAME`, `DATABRICKS_HTTP_PATH`, `DATABRICKS_CATALOG`, and `DATABRICKS_SCHEMA` environment variables, as described in [Personal access token authentication](https://docs.databricks.com/aws/en/dev-tools/sqlalchemy.md#pat).

```python
import os
from sqlalchemy import create_engine

server_hostname = os.getenv("DATABRICKS_SERVER_HOSTNAME")
http_path       = os.getenv("DATABRICKS_HTTP_PATH")
catalog         = os.getenv("DATABRICKS_CATALOG")
schema          = os.getenv("DATABRICKS_SCHEMA")

engine = create_engine(
  url = f"databricks://{server_hostname}?" +
        f"http_path={http_path}&catalog={catalog}&schema={schema}",
  connect_args = {"auth_type": "databricks-oauth"}
)

# ...
```

### <a id="oauth-m2m" />OAuth machine-to-machine (M2M) authentication

[OAuth M2M authentication](https://docs.databricks.com/aws/en/dev-tools/auth/oauth-m2m.md) authenticates as a Databricks service principal. You must also install the [Databricks SDK for Python](https://docs.databricks.com/aws/en/dev-tools/sdk-python.md) (for example, by running `pip install databricks-sdk`). For the Databricks SQL Connector for Python and SDK versions that support it on each cloud, see [OAuth machine-to-machine (M2M) authentication](https://docs.databricks.com/aws/en/dev-tools/python-sql-connector.md#auth-m2m).

To use OAuth M2M authentication, do the following:

1. Create a Databricks service principal in your Databricks workspace, and create an OAuth secret for that service principal. See [Authorize Databricks service principal access to Databricks with OAuth](https://docs.databricks.com/aws/en/dev-tools/auth/oauth-m2m.md).
2. Give that service principal access to your cluster or SQL warehouse. See [Compute permissions](https://docs.databricks.com/aws/en/compute/clusters-manage.md#cluster-level-permissions) or [Manage a SQL warehouse](https://docs.databricks.com/aws/en/compute/sql-warehouse/create.md#manage).

To authenticate the SQLAlchemy dialect with OAuth M2M, use the following code snippet. In addition to the `DATABRICKS_SERVER_HOSTNAME`, `DATABRICKS_HTTP_PATH`, `DATABRICKS_CATALOG`, and `DATABRICKS_SCHEMA` environment variables, this snippet assumes that you have set the following environment variables:

* `DATABRICKS_CLIENT_ID`, set to the service principal's **UUID** or **Application ID** value.
* `DATABRICKS_CLIENT_SECRET`, set to the **Secret** value for the service principal's OAuth secret.

```python
import os
from databricks.sdk.core import Config, oauth_service_principal
from sqlalchemy import create_engine

server_hostname = os.getenv("DATABRICKS_SERVER_HOSTNAME")
http_path       = os.getenv("DATABRICKS_HTTP_PATH")
catalog         = os.getenv("DATABRICKS_CATALOG")
schema          = os.getenv("DATABRICKS_SCHEMA")

def credential_provider():
  config = Config(
    host          = f"https://{server_hostname}",
    client_id     = os.getenv("DATABRICKS_CLIENT_ID"),
    client_secret = os.getenv("DATABRICKS_CLIENT_SECRET"))
  return oauth_service_principal(config)

engine = create_engine(
  url = f"databricks://{server_hostname}?" +
        f"http_path={http_path}&catalog={catalog}&schema={schema}",
  connect_args = {"credentials_provider": credential_provider}
)

# ...
```

## SQLAlchemy v1

For connection examples, refer this [example.py](https://github.com/databricks/databricks-sqlalchemy/blob/v1/main/example.py)

## SQLAlchemy v2

For connection examples, see the following section and the [sqlalchemy\_example.py](https://github.com/databricks/databricks-sqlalchemy/blob/main/sqlalchemy_example.py) file in GitHub.

## DBAPI reference

* [databricks-sqlalchemy v1](https://github.com/databricks/databricks-sqlalchemy/tree/v1/main) GitHub repository
* [SQLAlchemy v1 README.md](https://github.com/databricks/databricks-sqlalchemy/blob/v1/main/README.md)
* [databricks-sqlalchemy v2](https://github.com/databricks/databricks-sqlalchemy/tree/main) GitHub repository
* [SQLAlchemy v2 README.md](https://github.com/databricks/databricks-sqlalchemy/blob/main/README.md)

## Additional resources

* [SQLAlchemy 1 Unified Tutorial](https://docs.sqlalchemy.org/en/14/tutorial/index.html)
* [SQLAlchemy 2 Unified Tutorial](https://docs.sqlalchemy.org/en/20/tutorial/index.html)
