Connecting to ClickHouse with Python
In this example we will show how you can use the official Python driver for ClickHouse to connect to your cluster. Before you begin, it is imperative to review the general steps and considerations for connecting to a ClickHouse cluster as outlined in Connecting to a ClickHouse Cluster.
1. Install the clickhouse-connect driver for Python. You can either use the pip command or can download it from GitHub. For detailed installation instructions see https://github.com/ClickHouse/clickhouse-docs/blob/main/docs/en/integrations/language-clients/python/index.md
1 |
pip install clickhouse-connect |
or
1 |
git clone https://github.com/ClickHouse/clickhouse-connect |
2. Gather the host domain name and password for the icclickhouse user from the Connection Info page on console.
3. Replace them into the following script and you should be good to connect to your cluster.
1 2 3 4 5 |
import clickhouse_connect client = clickhouse_connect.get_client(host='<host-domain-name>', port=8443, username='icclickhouse', password='<password>') query_result = client.query('SHOW CLUSTERS') print (query_result.result_set) |