Connect to the Aiven for ClickHouse® service with Python#

To connect to your Aiven for ClickHouse® service with Python, you can use either the native protocol or the HTTPS protocol. This article provides you with instructions for both scenarios.

Connect with the native protocol#

Pre-requisites#

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 available in the Aiven console: Service Overview > Connection information > ClickHouse native

PASSOWRD

Password for the ClickHouse connection available in the Aiven console: Service Overview > Connection information > ClickHouse native

HOST

Host for the ClickHouse connection available in the Aiven console: Service Overview > Connection information > ClickHouse native

NATIVE_PORT

Port for the ClickHouse connection available in the Aiven console: Service Overview > Connection information > ClickHouse native

query

Query you want to run, for example SELECT 1

Connect to the service#

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

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#

Pre-requisites#

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 available in the Aiven console: Service Overview > Connection information > ClickHouse HTTPS & JDBC

query

Query you want to run, for example SELECT 1

X-ClickHouse-Database

Database Name available in the Aiven console: Service Overview > Connection information > ClickHouse HTTPS & JDBC, for example system

X-ClickHouse-User

User for the ClickHouse connection available in the Aiven console: Service Overview > Connection information > ClickHouse HTTPS & JDBC

X-ClickHouse-Key

Password for the ClickHouse connection available in the Aiven console: Service Overview > Connection information > ClickHouse HTTPS & JDBC

X-ClickHouse-Format

Format for the output from your query, for example JSONCompact

Connect to the service#

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

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)

Expected result

Now you have your service connection set up and you can proceed to uploading data into your database.

See also

For information on how to connect to the Aiven for Clickhouse service with the ClickHouse client, see Connect with the ClickHouse client.