SoFunction
Updated on 2025-04-26

Python uses SQLAlchemy to connect to ClickHouse database

ClickHouse is an open source columnar database management system, known for its high-speed real-time data analysis capabilities. SQLAlchemy is a Python SQL toolkit and object relational mapping (ORM) library that simplifies interaction with databases.

Prerequisites

Before you start, make sure you have installed the following components:

  • Python: Version 3.6 or higher.
  • ClickHouse: An instance of ClickHouse running on a local or remote server.
  • SQLAlchemy: Install SQLAlchemy via pip:
pip install sqlalchemy

ClickHouse SQLAlchemy driver: ClickHouse's SQLAlchemy driver needs to be installed to connect to ClickHouse through SQLAlchemy:

pip install clickhouse-sqlalchemy

Step 1: Configure the connection

First, we need to configure the connection settings of the ClickHouse database. In the provided code snippet, the connection settings are taken from a configuration dictionary. Here is a sample configuration:

settings = {
    'CLICKHOUSE_DATABASE': {
        'username': 'default',
        'password': '',
        'host': '192.168.1.202',
        'port': '8123',
        'database': 'shujujiance'
    }
}

# Step 2: Generate ClickHouse URINext,We use the information in the configuration to generate the connection URI。this URI Will include the username、password、Host address、Port number and database name。The following is generated URI Code:

```python
clickhouseConfig = settings['CLICKHOUSE_DATABASE']
clickhouseURI = f"clickhouse+http://{('username')}:{('password')}@{('host')}:{('port')}/{('database')}"
print(clickhouseURI)

The generated clickhouseURI may be similar to the following format:

clickhouse+http://default:@192.168.1.202:8123/shujujiance

Step 3: Create a connection engine using SQLAlchemy

Once we have the correct connection URI, we can use SQLAlchemy's create_engine function to create the connection engine. This engine will be used to perform SQL queries and operations.

from sqlalchemy import create_engine
from  import sessionmaker

# Create a database engineengine = create_engine(clickhouseURI)

# Create a new session classSession = sessionmaker(bind=engine)

# Create a session objectsession = Session()

Step 4: Perform database operations

Now that we have successfully connected to the ClickHouse database, we can perform various database operations through SQLAlchemy. For example, querying data or inserting new records, etc.

Here is a simple query example:

result = ("SELECT * FROM your_table_name")
for row in result:
    print(row)

Summarize

Through this article, you should learn how to configure and connect to the ClickHouse database using SQLAlchemy. SQLAlchemy provides powerful functions that not only simplify database operations, but also can be used in combination with an efficient analytical database like ClickHouse to handle large-scale data analysis tasks.

This is the article about Python using SQLAlchemy to connect to ClickHouse database. For more related Python SQLAlchemy to connect to ClickHouse, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!