Skip to content

Configuration ​

A ClickHouse connection uses the clickhouse driver. The package merges its defaults into config('database.connections.clickhouse'). The values of config/clickhouse.php override the defaults. A connection in config/database.php overrides both. See Installation.

Options ​

Option.env variableDefaultDescription
hostCLICKHOUSE_HOST127.0.0.1Host of the server
portCLICKHOUSE_PORT8123HTTP port of the server
databaseCLICKHOUSE_DATABASEdefaultDatabase of the connection
usernameCLICKHOUSE_USERNAMEdefaultUser name
passwordCLICKHOUSE_PASSWORD''Password
httpsCLICKHOUSE_HTTPSfalseUse HTTPS
timeout_connectCLICKHOUSE_TIMEOUT_CONNECT2Seconds to wait for a connection. See Timeouts.
timeout_queryCLICKHOUSE_TIMEOUT_QUERY2Seconds that a request can take. See Timeouts.
retriesCLICKHOUSE_RETRIES0Number of retries of a failed request. See Retries.
retry_onCLICKHOUSE_RETRY_ONreadsreads, any or unsent: the failed requests that the retries send again. See Retries.
settings[]ClickHouse settings for each query of the connection
use_lightweight_deleteCLICKHOUSE_USE_LIGHTWEIGHT_DELETEfalsedelete() without an argument sends a lightweight DELETE FROM. See Deletions.
engineCLICKHOUSE_ENGINEnullEngine of a table that Schema::create() creates without engine(). null is MergeTree().
default_index_typeCLICKHOUSE_DEFAULT_INDEX_TYPEnullType of a data-skipping index that has no type, such as minmax
datetime_precisionCLICKHOUSE_DATETIME_PRECISIONsecondsecond or microsecond, or a DateTimePrecision case. See Dates and times.
insert_formatCLICKHOUSE_INSERT_FORMATValuesValues or JSONEachRow, or a Format case. See JSONEachRow inserts.
use_on_clusterCLICKHOUSE_USE_ON_CLUSTERfalseSend delete(), update(), truncate() and optimize() ON CLUSTER. It needs cluster_name.
clusterList of nodes. It replaces host and port. See Clusters.
cluster_nameCluster name of the server config, for ON CLUSTER. See Clusters.

The connection passes its config to the smi2 client, so the client's own options work too: sslCA, the CA file of an HTTPS server, auth_method, curl_options and readonly.

The options datetime_precision, insert_format and use_on_cluster are off by default. Without them, the package writes dates with seconds, inserts in Values and sends mutations to the active node.

DB::connection('clickhouse')->table() and query() return the package's QueryBuilder, Laravel's query builder with the ClickHouse clauses. No option changes it. See Query builder basics.

Validation ​

The connection checks its options when Laravel creates it, before it pings a node. An incorrect value throws an InvalidArgumentException:

text
The [datetime_precision] option of the ClickHouse connection [clickhouse] must be 'second' or 'microsecond', [millisecond] given.

use_on_cluster without cluster_name also throws.

datetime_precision and insert_format take a string in any letter case, with or without surrounding spaces, or a case of the Oralunal\LaravelClickHouse\Enums\DateTimePrecision and Oralunal\LaravelClickHouse\Enums\Format enums:

php
use Oralunal\LaravelClickHouse\Enums\DateTimePrecision;
use Oralunal\LaravelClickHouse\Enums\Format;

'datetime_precision' => DateTimePrecision::Microsecond,
'insert_format' => Format::JsonEachRow,

Settings ​

The settings option sends ClickHouse settings with each query of the connection:

php
'settings' => [
    'max_partitions_per_insert_block' => 300,
],

To set a setting for one query, use settings() of the query builder.

Read the options ​

MethodReturns
getDateTimePrecision()DateTimePrecision::Second or DateTimePrecision::Microsecond
getInsertFormat()Format::Values or Format::JsonEachRow
formatDateTime($date)A date as the connection writes it, without quotes: 2024-01-02 03:04:05, or 2024-01-02 03:04:05.500000 at microsecond
getClusterName()The cluster_name option without surrounding spaces, or null when it is blank
hasClusterNodes()true when the cluster option lists nodes
getDefaultCluster()The cluster of ON CLUSTER when use_on_cluster is on, or null. See Clusters.
getServerVersion()The version of the server of the active node, for example 24.8.14.39
getDriverTitle()ClickHouse, the name that php artisan db:show shows
newLiteralGrammar()A LiteralGrammar that writes values as ClickHouse literals, as the connection writes them, with dates at datetime_precision. See Literals.

Call them on the connection: DB::connection('clickhouse')->getServerVersion(). Connection::createWithClient($config) makes a connection and its nodes from a config array. The clickhouse driver uses it. It checks the options before it pings a node.

Released under the MIT License.