# Connecting to Trino

> Learn how to connect to Trino using Superset, trino-cli, DBeaver, Python or Power BI. Includes setup for SSL/TLS, OpenID Connect, and basic authentication.

Trino offers an SQL endpoint via a HTTP API. Trino provides an [open-source JDBC driver](https://trino.io/docs/current/client/jdbc.html). For ODBC, Stackable provides the open-source [Stackable ODBC driver for Trino](https://github.com/stackabletech/stackable-odbc-trino), which is not part of SDP and isn’t covered by Stackable support.

Multiple SQL clients can be used to connect to Trino as described below.

## Prerequisites

The Trino server must be reachable from your local computer. It is typically secured via HTTPS with a certificated provided by a [SecretClass](https://docs.stackable.tech/home/stable/secret-operator/secretclass/index.md).

As most setups use a self-signed certificate, that cannot be verified without the CA certificate, the easiest way is to disable the Trino cert validation as the following guide does, however this is not secure! To use certificate validation instead, you need to extract the `ca.crt` file from the the SecretClass and provide it to the Trino client.

## Connect with Superset

In Superset, a `Database Connection` to Trino can be configured via the API or UI. Set the `SQLALCHEMY URI` in the `Basic` tab.

![connect with superset 1](https://docs.stackable.tech/home/stable/trino/_images/connect-with-superset-1.png)

Stackable Trino clusters use HTTPS per default. Therefore additional settings must be configured.

### Set `http_scheme`

In the `Advanced` tab under `Other` in the `ENGINE PARAMETERS` settings, set the `http_scheme` explicitly to `https`.

```json
{"connect_args":{"http_scheme":"https"}}
```

![connect with superset 2](https://docs.stackable.tech/home/stable/trino/_images/connect-with-superset-2.png)

### Set `ROOT CERTIFICATE`

Additionally, in the `Advanced` tab under `Security` in the `ROOT CERTIFICATE`, you may want to set a CA Bundle for e.g. self-signed certificates.

### Skip TLS verification

> **Warning:** This deactivates TLS verification and should only be used for testing purposes and never in production!

In the `Advanced` tab under `Other` in the `ENGINE PARAMETERS`, set `verify` to `false`.

```json
{"connect_args":{"verify":false,"http_scheme":"https"}}
```

## Connect with trino-cli

Consult the [official trino-cli docs](https://trino.io/docs/current/client/cli.html) for details on how to connect to a running Trino cluster. The `--insecure` flag ignores the server TLS certificate and is required in this case, so your command looks something like:

```console
$ trino-cli-481 --server https://85.215.195.29:8443 --user admin --password --insecure
```

> **Tip:** In case you are using OpenID connect, use `--external-authentication` instead of `--password`. A browser window will be opened, which might require you to log in. You still need to pass the `--user` argument because of [this Trino issue](https://github.com/trinodb/trino/issues/11547).

## Connect with DBeaver

[DBeaver](https://dbeaver.io) is a free multi-platform database tool for anyone who needs to work with databases. It is installed locally and can connect to Trino and offers a convenient UI to explore and work with Trino.

First of you need to click on the _New Database Connection_ icon, select _Other_ > _Trino_.

![connect with dbeaver 1](https://docs.stackable.tech/home/stable/trino/_images/connect-with-dbeaver-1.png)

Afterwards you need to enter the _Host_ and _Port_ as well as _Username_ and _Password_.

![connect with dbeaver 2](https://docs.stackable.tech/home/stable/trino/_images/connect-with-dbeaver-2.png)

After entering the details you need to switch to the tab called _Driver properties_. First action is to click on the `SSL` driver property and enter the value `true`. Additionally you need to click on the green plus icon in the bottom left corner to add a _User Property_ called `SSLVerification`. Afterwards you need to set the value to `NONE`.

![connect with dbeaver 3](https://docs.stackable.tech/home/stable/trino/_images/connect-with-dbeaver-3.png)

As the last step you can click on _Finish_ and start using the Trino connection.

> **Tip:** In case you are using OpenID connect, set the `externalAuthentication` property to `true` and don’t provide and username or password. A browser window will be opened, which might require you to log in.

## Connect with Power BI

[Connecting Power BI to Trino](https://docs.stackable.tech/home/stable/guides/connecting-power-bi/index.md) covers Power BI Desktop, the Power BI Service and Power BI Report Server.

## Connect with Python

For more information on how to connect to Trino from Python, have a look at the official [trino-python-client](https://github.com/trinodb/trino-python-client).

A minimal example of making a connection and running a query looks like this:

```python
import trino

def get_connection():
    connection = trino.dbapi.connect(
        host="trino-coordinator-default.default.svc.cluster.local",
        http_scheme="https",
        verify=False, # For best security you can also provide a path to the trino root ca: "/stackable/secrets/trino-ca-cert/ca.crt",
        port=8443,
        user="admin",
        auth=trino.auth.BasicAuthentication("admin", "adminadmin"),
        catalog="hive",
        schema="staging",
    )
    return connection

def run_query(connection, query):
    # print(f"[DEBUG] Executing query {query}")
    cursor = connection.cursor()
    cursor.execute(query)
    return cursor.fetchall()

connection = get_connection()

run_query(connection, "show catalogs")
```

In case you are using OpenID connect use the following connection definition:

```python
    connection = trino.dbapi.connect(
        host="87.106.119.4", # You need to use an an externally reachable address, so that your browser can access it!
        port=8443,
        http_scheme="https",
        verify=False, # For best security you can also provide a path to the trino root ca: "/stackable/secrets/trino-ca-cert/ca.crt",
        auth=trino.auth.OAuth2Authentication(),
        catalog="hive",
        schema="staging",
    )
```

This snippet prints the following statement

```
Open the following URL in browser for the external authentication:
https://87.106.119.4:8443/oauth2/token/initiate/52787f3f7e18791121e3123eedcd797a06c0870548c007cd96e6e82899fbf830
```

Click on the link, potentially log in and you should be authenticated.
