pip install clickhouse-connect★Optional extras:[numpy],[pandas],[arrow],[async],[sqlalchemy]. Python 3.9+.import clickhouse_connect client = clickhouse_connect.get_client( host="localhost", port=8123, username="default", password="")★The entry point. Default HTTP port is 8123; for TLS usesecure=True(port 8443).client = clickhouse_connect.get_client( host="<id>.clickhouse.cloud", secure=True, password="...")ClickHouse Cloud:secure=True+ host from the console. Adddatabase=to set the default DB.client.ping() · client.server_versionVerify the connection.client.close()when done (or use it as a context manager).
res = client.query("SELECT id, name FROM t WHERE id > 10")★Returns aQueryResult.res.result_rows= list of row tuples;res.column_names= the headers.for row in res.result_rows: print(row) # (id, name)★Iterate rows as tuples.res.result_columnsgives column-oriented data instead.client.query("SELECT count() FROM t").result_rows[0][0]Pull a single scalar.res.summaryhas rows-read / bytes-read stats.rows = client.query_np("SELECT * FROM t") # NumPy arrayquery_npreturns a NumPy structured array — efficient for numeric analytics.
df = client.query_df("SELECT * FROM t WHERE ts >= today()")★query_dfreturns a pandas DataFrame directly — the most common analytics path. Needs the pandas extra.tbl = client.query_arrow("SELECT * FROM t") # pyarrow.Table★query_arrowreturns an Arrow Table — zero-copy handoff to Polars/DuckDB/Parquet.for df in client.query_df_stream("SELECT * FROM big"): process(df)Stream results as a sequence of DataFrame blocks — constant memory over huge tables (card 9).