# Connect to Managed ClickHouse® with Python

To connect to your Managed ClickHouse® service with Python, you can
use either the native protocol or the HTTPS protocol.

## Connect with the native protocol

### Prerequisites

-   [Python 3.9 or later](https://www.python.org/downloads/)
-   [ClickHouse Python
    Driver](https://pypi.org/project/clickhouse-driver/)

### Identify connection information

To run the code for connecting to your service, first identify values of
the following variables:

 |   Variable    |             Description              |
 |---------------|--------------------------------------|
 | `USERNAME`    | `User` for the ClickHouse connection |
 | `PASSWORD`    | `Password` for the ClickHouse connection |
 | `HOST`        | `Host` for the ClickHouse connection |
 | `NATIVE_PORT` | `Port` for the ClickHouse connection |
 | `query`       | Query to run, for example `SELECT 1` |

Read the host and the native port from the `clickhouse` entry of the service
components. Ports are assigned per service, so read them from your own service
rather than reusing a value from an example:

```bash
exo x get-dbaas-service-clickhouse my-clickhouse -z ch-gva-2 \
  -q "components[?component=='clickhouse']"
```

### Connect to the service

Replace the placeholders in the code with meaningful information on your
service connection and run the code.

```python
from clickhouse_driver import Client
client = Client(user="USERNAME", password="PASSWORD", host="HOST", port=NATIVE_PORT, secure=True)
print(client.execute("SELECT 1"))
```

## Connect with HTTPS

### Prerequisites

-   [Python 3.10 or later](https://www.python.org/downloads/)
-   [Requests HTTP library](https://pypi.org/project/requests/)

### Identify connection information

To run the code for connecting to your service, first identify values of
the following variables:

|         Variable          |                           Description                            |
|---------------------------|------------------------------------------------------------------|
| `https://HOST:HTTPS_PORT` | `Host` and `Port` for the ClickHouse connection                  |
| `query`                   | Query to run, for example `SELECT 1`                             |
| `X-ClickHouse-Database`   | `Database Name`, for example `system`                            |
| `X-ClickHouse-User`       | `User` for the ClickHouse connection                             |
| `X-ClickHouse-Key`        | `Password` for the ClickHouse connection                         |
| `X-ClickHouse-Format`     | Format for the output from your query, for example `JSONCompact` |

The HTTPS endpoint listens on a different port than the native protocol. Read the
host and the port from the `clickhouse_https` entry of the service components:

```bash
exo x get-dbaas-service-clickhouse my-clickhouse -z ch-gva-2 \
  -q "components[?component=='clickhouse_https']"
```

### Connect to the service

Replace the placeholders in the code with meaningful information on your
service connection and run the code.

```python
import requests
response = requests.post(
    "https://HOST:HTTPS_PORT",
    params={"query": "SELECT 1"},
    headers={
        "X-ClickHouse-Database": "system",
        "X-ClickHouse-User": "USERNAME",
        "X-ClickHouse-Key": "PASSWORD",
        "X-ClickHouse-Format": "JSONCompact",
    })
print(response.text)
```

Now you have your service connection set up and you can proceed to
[uploading data into your database](/product/dbaas/service-specific/clickhouse/quick-start/#load-a-dataset).

For information on how to connect to the Managed ClickHouse service
with the ClickHouse client, see
[Connect with the ClickHouse client](/product/dbaas/service-specific/clickhouse/how-to/connect-with-clickhouse-cli/).

