PHPackages                             valentin-auclerc/dbal-clickhouse - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. valentin-auclerc/dbal-clickhouse

ActiveLibrary[Database &amp; ORM](/categories/database)

valentin-auclerc/dbal-clickhouse
================================

Doctrine DBAL driver for ClickHouse

v3.0.1(1y ago)02MITPHPPHP ^8.1

Since Mar 20Pushed 1y agoCompare

[ Source](https://github.com/ValentinAUCLERC/dbal-clickhouse)[ Packagist](https://packagist.org/packages/valentin-auclerc/dbal-clickhouse)[ Docs](https://github.com/friendsofdoctrine/dbal-clickhouse)[ Fund](https://ko-fi.com/FriendsOfDoctrine)[ RSS](/packages/valentin-auclerc-dbal-clickhouse/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (4)Versions (26)Used By (0)

Doctrine DBAL ClickHouse Driver
===============================

[](#doctrine-dbal-clickhouse-driver)

[![Latest Version on Packagist](https://camo.githubusercontent.com/eb8217cf1197ee1e40cc58cacd752b124de7abb3f0b5ad63bb735f09f3e4c03f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f467269656e64734f66446f637472696e652f6462616c2d636c69636b686f7573652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/FriendsOfDoctrine/dbal-clickhouse)[![GitHub Tests Status](https://camo.githubusercontent.com/cde987bbcfc4c7ccde6dd98619afb8e7e29e83a9fa9d7b67b9113d5724f196f8/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f467269656e64734f66446f637472696e652f6462616c2d636c69636b686f7573652f74657374732e796d6c3f6272616e63683d6d6173746572266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/FriendsOfDoctrine/dbal-clickhouse/actions/workflows/tests.yml?query=branch%3Amaster)[![GitHub Code Style Status](https://camo.githubusercontent.com/cbf920f737d8d7da350492aa4a4e99929bd7bde476bec582b4efff159856a4a4/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f467269656e64734f66446f637472696e652f6462616c2d636c69636b686f7573652f7068707374616e2e796d6c3f6272616e63683d6d6173746572266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/FriendsOfDoctrine/dbal-clickhouse/actions/workflows/phpstan.yml?query=branch%3Amaster)[![Total Downloads](https://camo.githubusercontent.com/430471ba72a5319b890b29c952014b79a455e9b2dba3d759b9beaa5a8e15b8ca/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f467269656e64734f66446f637472696e652f6462616c2d636c69636b686f7573652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/FriendsOfDoctrine/dbal-clickhouse)[![Licence](https://camo.githubusercontent.com/722b385bdf75faf593ca1b5ac115df4d4886db098ca3f945da8e720a469fd3d8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f467269656e64734f66446f637472696e652f6462616c2d636c69636b686f7573652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/FriendsOfDoctrine/dbal-clickhouse)

Doctrine DBAL driver for ClickHouse - an open-source column-oriented database management system by [Yandex](https://clickhouse.yandex/)

**Driver is suitable for Symfony or any other framework using Doctrine.**

- [v3](https://github.com/FriendsOfDoctrine/dbal-clickhouse/releases?q=v3.) supports Doctrine DBAL 4+ and PHP 8.1+
- [v2](https://github.com/FriendsOfDoctrine/dbal-clickhouse/releases?q=v2.) supports Doctrine DBAL 3+ and PHP 8.0+
- [v1](https://github.com/FriendsOfDoctrine/dbal-clickhouse/releases?q=v1.) supports Doctrine DBAL 2+ and PHP 7.1+

Installation
------------

[](#installation)

```
composer require friendsofdoctrine/dbal-clickhouse

```

Initialization
--------------

[](#initialization)

### Custom PHP script

[](#custom-php-script)

```
$connectionParams = [
    'host' => 'localhost',
    'port' => 8123,
    'user' => 'default',
    'password' => '',
    'dbname' => 'default',
    'driverClass' => 'FOD\DBALClickHouse\Driver',
    'wrapperClass' => 'FOD\DBALClickHouse\Connection',
    'driverOptions' => [
        'extremes' => false,
        'readonly' => true,
        'max_execution_time' => 30,
        'enable_http_compression' => 0,
        'https' => false,
    ],
];
$conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, new \Doctrine\DBAL\Configuration());
```

`driverOptions` are special `smi2/phpclickhouse` client [settings](https://github.com/smi2/phpClickHouse#settings)

### Symfony

[](#symfony)

configure...

```
# app/config/config.yml
doctrine:
    dbal:
        connections:
            clickhouse:
                host:     localhost
                port:     8123
                user:     default
                password: ""
                dbname:   default
                driver_class: FOD\DBALClickHouse\Driver
                wrapper_class: FOD\DBALClickHouse\Connection
                options:
                    enable_http_compression: 1
                    max_execution_time: 60
                    sslCA: '/usr/local/share/ca-certificates/Yandex/YandexInternalRootCA.crt'
            #mysql:
            #   ...
```

...and get from the service container

```
$conn = $this->get('doctrine.dbal.clickhouse_connection');
```

Usage
-----

[](#usage)

### Create database

[](#create-database)

```
php bin/console doctrine:database:create --connection=clickhouse --if-not-exists
```

### Create new table

[](#create-new-table)

```
// ***quick start***
$fromSchema = $conn->getSchemaManager()->createSchema();
$toSchema = clone $fromSchema;

// create new table object
$newTable = $toSchema->createTable('new_table');

// add columns
$newTable->addColumn('id', 'integer', ['unsigned' => true]);
$newTable->addColumn('payload', 'string', ['notnull' => false]);
// *option 'notnull' in false mode allows you to insert NULL into the column;
//                   in this case, the column will be represented in the ClickHouse as Nullable(String)
$newTable->addColumn('hash', 'string', ['length' => 32, 'fixed' => true]);
// *option 'fixed' sets the fixed length of a string column as specified;
//                 if specified, the type of the column is FixedString

//set primary key
$newTable->setPrimaryKey(['id']);

// execute migration SQLs to create table in ClickHouse
$sqlArray = $fromSchema->getMigrateToSql($toSchema, $conn->getDatabasePlatform());
foreach ($sqlArray as $sql) {
    $conn->exec($sql);
}
```

```
// ***more options (optional)***

//specify table engine
$newTable->addOption('engine', 'MergeTree');
// *if not specified -- default engine 'ReplacingMergeTree' will be used

// add Date column for partitioning
$newTable->addColumn('event_date', 'date', ['default' => 'toDate(now())']);
$newTable->addOption('eventDateColumn', 'event_date');
// *if not specified -- default Date column named EventDate will be added
$newTable->addOption('eventDateProviderColumn', 'updated_at');
// *if specified -- event date column will be added with default value toDate(updated_at);
//    if the type of the provider column is `string`, the valid format of provider column values must be either `YYYY-MM-DD` or `YYYY-MM-DD hh:mm:ss`
//    if the type of provider column is neither `string`, nor `date`, nor `datetime`, provider column values must contain a valid UNIX Timestamp
$newTable->addOption('samplingExpression', 'intHash32(id)');
// samplingExpression -- a tuple that defines the table's primary key, and the index granularity

//specify index granularity
$newTable->addOption('indexGranularity', 4096);
// *if not specified -- default value 8192 will be used
```

### Insert

[](#insert)

```
// 1
$conn->exec("INSERT INTO new_table (id, payload) VALUES (1, 'dummyPayload1')");
```

```
// 2
$conn->insert('new_table', ['id' => 2, 'payload' => 'dummyPayload2']);
// INSERT INTO new_table (id, payload) VALUES (?, ?) [2, 'dummyPayload2']
```

```
// 3 via QueryBuilder
$qb = $conn->createQueryBuilder();

$qb
    ->insert('new_table')
    ->setValue('id', ':id')
    ->setValue('payload', ':payload')
    ->setParameter('id', 3, \PDO::PARAM_INT) // need to explicitly set param type to `integer`, because default type is `string` and ClickHouse doesn't like types mismatchings
    ->setParameter('payload', 'dummyPayload3');

$qb->execute();
```

### Select

[](#select)

```
echo $conn->fetchColumn('SELECT SUM(views) FROM articles');
```

### Select via Dynamic Parameters and Prepared Statements

[](#select-via-dynamic-parameters-and-prepared-statements)

```
$stmt = $conn->prepare('SELECT authorId, SUM(views) AS total_views FROM articles WHERE category_id = :categoryId AND publish_date = :publishDate GROUP BY authorId');

$stmt->bindValue('categoryId', 123);
$stmt->bindValue('publishDate', new \DateTime('2017-02-29'), 'datetime');
$stmt->execute();

while ($row = $stmt->fetch()) {
    echo $row['authorId'] . ': ' . $row['total_views'] . PHP_EOL;
}
```

### Additional types

[](#additional-types)

If you want to use [Array(T) type](https://clickhouse.yandex/reference_en.html#Array(T)), register additional DBAL types in your code:

```
// register all custom DBAL Array types
ArrayType::registerArrayTypes($conn->getDatabasePlatform());
// register one custom DBAL Array(Int8) type
Type::addType('array(int8)', 'FOD\DBALClickHouse\Types\ArrayInt8Type');
```

or register them in Symfony configuration file:

```
# app/config/config.yml
doctrine:
    dbal:
        connections:
        ...
        types:
            array(int8): FOD\DBALClickHouse\Types\ArrayInt8Type
            array(int16): FOD\DBALClickHouse\Types\ArrayInt16Type
            array(int32): FOD\DBALClickHouse\Types\ArrayInt32Type
            array(int64): FOD\DBALClickHouse\Types\ArrayInt64Type
            array(uint8): FOD\DBALClickHouse\Types\ArrayUInt8Type
            array(uint16): FOD\DBALClickHouse\Types\ArrayUInt16Type
            array(uint32): FOD\DBALClickHouse\Types\ArrayUInt32Type
            array(uint64): FOD\DBALClickHouse\Types\ArrayUInt64Type
            array(float32): FOD\DBALClickHouse\Types\ArrayFloat32Type
            array(float64): FOD\DBALClickHouse\Types\ArrayFloat64Type
            array(string): FOD\DBALClickHouse\Types\ArrayStringableType
            array(datetime): FOD\DBALClickHouse\Types\ArrayDateTimeType
            array(date): FOD\DBALClickHouse\Types\ArrayDateType
```

Additional type `BigIntType` helps you to store bigint values as [Int64/UInt64](https://clickhouse.yandex/reference_en.html#UInt8,%20UInt16,%20UInt32,%20UInt64,%20Int8,%20Int16,%20Int32,%20Int64) value type in ClickHouse. You can override DBAL type in your code:

```
Type::overrideType(Type::BIGINT, 'FOD\DBALClickHouse\Types\BigIntType');
```

or use custom mapping types in Symfony configuration:

```
# app/config/config.yml
doctrine:
    dbal:
        types:
            bigint:  FOD\DBALClickHouse\Types\BigIntType
            ...
```

### More information in Doctrine DBAL documentation:

[](#more-information-in-doctrine-dbal-documentation)

- [Data Retrieval And Manipulation](http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/data-retrieval-and-manipulation.html)
- [SQL Query Builder](http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/query-builder.html)
- [Schema-Representation](http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/schema-representation.html)

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance41

Moderate activity, may be stable

Popularity2

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity82

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 66.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~119 days

Recently: every ~95 days

Total

24

Last Release

593d ago

Major Versions

0.2.2 → 1.0.02017-04-17

v1.5.6 → v2.0.02023-09-13

v2.0.0 → v3.0.02024-09-26

v2.x-dev → v3.0.12024-09-26

PHP version history (5 changes)0.1.2PHP &gt;=5.4.0

v1.2.0PHP ^7.1

v1.5.4PHP ^7.1 || ^8.0

v2.0.0PHP ^8.0

v3.0.0PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/7bd6b3de49b79657fdcc1f5531a795a2066ead69589944a7eeb8c7233e0156f6?d=identicon)[ValentinAUCLERC](/maintainers/ValentinAUCLERC)

---

Top Contributors

[![mochalygin](https://avatars.githubusercontent.com/u/2054744?v=4)](https://github.com/mochalygin "mochalygin (117 commits)")[![argayash](https://avatars.githubusercontent.com/u/790535?v=4)](https://github.com/argayash "argayash (46 commits)")[![asanikovich](https://avatars.githubusercontent.com/u/561064?v=4)](https://github.com/asanikovich "asanikovich (7 commits)")[![ValentinAUCLERC](https://avatars.githubusercontent.com/u/4924200?v=4)](https://github.com/ValentinAUCLERC "ValentinAUCLERC (3 commits)")[![pschombara](https://avatars.githubusercontent.com/u/26956824?v=4)](https://github.com/pschombara "pschombara (1 commits)")[![Vasary](https://avatars.githubusercontent.com/u/2060334?v=4)](https://github.com/Vasary "Vasary (1 commits)")

---

Tags

doctrinedbaldriverclickhouse

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/valentin-auclerc-dbal-clickhouse/health.svg)

```
[![Health](https://phpackages.com/badges/valentin-auclerc-dbal-clickhouse/health.svg)](https://phpackages.com/packages/valentin-auclerc-dbal-clickhouse)
```

###  Alternatives

[friendsofdoctrine/dbal-clickhouse

Doctrine DBAL driver for ClickHouse

1141.2M1](/packages/friendsofdoctrine-dbal-clickhouse)[martin-georgiev/postgresql-for-doctrine

Extends Doctrine with native PostgreSQL support for arrays, JSONB, ranges, PostGIS geometries, text search, ltree, uuid, and 100+ PostgreSQL-specific functions.

4485.3M4](/packages/martin-georgiev-postgresql-for-doctrine)[nemo64/dbal-rds-data

rds-data driver for doctrine dbal

2713.2k](/packages/nemo64-dbal-rds-data)[flow-php/doctrine-dbal-bulk

Bulk inserts and updates for Doctrine DBAL

14295.2k1](/packages/flow-php-doctrine-dbal-bulk)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
