clickhouse-client
clickhouse-client
ClickHouse provides a native command-line client: clickhouse-client
. The client supports command-line options and configuration files.
Install it from the clickhouse-client
package and run it with the command clickhouse-client
.
$ clickhouse-client
ClickHouse client version 20.13.1.5273 (official build).
Connecting to localhost:9000 as user default.
Connected to ClickHouse server version 20.13.1.
:)
Different client and server versions are compatible with one another, but some features may not be available in older clients. We recommend using the same version of the client as the server app. When you try to use a client of the older version, then the server, clickhouse-client
displays the message:
ClickHouse client version is older than ClickHouse server.
It may lack support for new features.
Usage
The client can be used in interactive and non-interactive (batch) mode.
Gather your connection details
To connect to ClickHouse with native TCP you need this information:
-
The HOST and PORT: typically, the port is 9440 when using TLS, or 9000 when not using TLS.
-
The DATABASE NAME: out of the box there is a database named
default
, use the name of the database that you want to connect to. -
The USERNAME and PASSWORD: out of the box the username is
default
. Use the username appropriate for your use case.
The details for your ClickHouse Cloud service are available in the ClickHouse Cloud console. Select the service that you will connect to and click Connect:
Choose Native, and the details are available in an example clickhouse-client
command.
If you are using self-managed ClickHouse, the connection details are set by your ClickHouse administrator.
Interactive
To connect to your ClickHouse Cloud service or any ClickHouse server using TLS and passwords, specify port 9440
(or --secure
) and provide your username and password:
clickhouse-client --host <HOSTNAME> \
--port 9440 \
--user <USERNAME> \
--password <PASSWORD>
To connect to a self-managed ClickHouse server you will need the details for that server. Whether or not TLS is used, port numbers, and passwords are all configurable. Use the above example for ClickHouse Cloud as a starting point.
Batch
To use batch mode, specify the ‘query’ parameter, or send data to ‘stdin’ (it verifies that ‘stdin’ is not a terminal), or both. Similar to the HTTP interface, when using the ‘query’ parameter and sending data to ‘stdin’, the request is a concatenation of the ‘query’ parameter, a line feed, and the data in ‘stdin’. This is convenient for large INSERT queries.
Examples of using the client to insert data:
Inserting a CSV file into a remote ClickHouse service
This example is appropriate for ClickHouse Cloud, or any ClickHouse server using TLS and a password. In this example a sample dataset CSV file, cell_towers.csv
is inserted into an existing table cell_towers
in the default
database:
clickhouse-client --host HOSTNAME.clickhouse.cloud \
--port 9440 \
--user default \
--password PASSWORD \
--query "INSERT INTO cell_towers FORMAT CSVWithNames" \
< cell_towers.csv
To concentrate on the query syntax, the rest of the examples leave off the connection details (--host
, --port
, etc.). Add them in when you try the commands.
Three different ways of inserting data
echo -ne "1, 'some text', '2016-08-14 00:00:00'\n2, 'some more text', '2016-08-14 00:00:01'" | \
clickhouse-client --database=test --query="INSERT INTO test FORMAT CSV";
cat <<_EOF | clickhouse-client --database=test --query="INSERT INTO test FORMAT CSV";
3, 'some text', '2016-08-14 00:00:00'
4, 'some more text', '2016-08-14 00:00:01'
_EOF
cat file.csv | clickhouse-client --database=test --query="INSERT INTO test FORMAT CSV";
Notes
In batch mode, the default data format is TabSeparated. You can set the format in the FORMAT clause of the query.
By default, you can only process a single query in batch mode. To make multiple queries from a “script,” use the --multiquery
parameter. This works for all queries except INSERT. Query results are output consecutively without additional separators. Similarly, to process a large number of queries, you can run ‘clickhouse-client’ for each query. Note that it may take tens of milliseconds to launch the ‘clickhouse-client’ program.
In interactive mode, you get a command line where you can enter queries.
If ‘multiline’ is not specified (the default): To run the query, press Enter. The semicolon is not necessary at the end of the query. To enter a multiline query, enter a backslash \
before the line feed. After you press Enter, you will be asked to enter the next line of the query.
If multiline is specified: To run a query, end it with a semicolon and press Enter. If the semicolon was omitted at the end of the entered line, you will be asked to enter the next line of the query.
Only a single query is run, so everything after the semicolon is ignored.
You can specify \G
instead of or after the semicolon. This indicates Vertical format. In this format, each value is printed on a separate line, which is convenient for wide tables. This unusual feature was added for compatibility with the MySQL CLI.
The command line is based on ‘replxx’ (similar to ‘readline’). In other words, it uses the familiar keyboard shortcuts and keeps a history. The history is written to ~/.clickhouse-client-history
.
By default, the format used is PrettyCompact. You can change the format in the FORMAT clause of the query, or by specifying \G
at the end of the query, using the --format
or --vertical
argument in the command line, or using the client configuration file.
To exit the client, press Ctrl+D, or enter one of the following instead of a query: “exit”, “quit”, “logout”, “exit;”, “quit;”, “logout;”, “q”, “Q”, “:q”
When processing a query, the client shows:
- Progress, which is updated no more than 10 times per second (by default). For quick queries, the progress might not have time to be displayed.
- The formatted query after parsing, for debugging.
- The result in the specified format.
- The number of lines in the result, the time passed, and the average speed of query processing. All data amounts refer to uncompressed data.
You can cancel a long query by pressing Ctrl+C. However, you will still need to wait for a little for the server to abort the request. It is not possible to cancel a query at certain stages. If you do not wait and press Ctrl+C a second time, the client will exit.
The command-line client allows passing external data (external temporary tables) for querying. For more information, see the section “External data for query processing”.
Queries with Parameters
You can create a query with parameters and pass values to them from client application. This allows to avoid formatting query with specific dynamic values on client side. For example:
$ clickhouse-client --param_parName="[1, 2]" -q "SELECT * FROM table WHERE a = {parName:Array(UInt16)}"
It is also possible to set parameters from within an interactive session:
$ clickhouse-client -nq "
SET param_parName='[1, 2]';
SELECT {parName:Array(UInt16)}"
Query Syntax
Format a query as usual, then place the values that you want to pass from the app parameters to the query in braces in the following format:
{<name>:<data type>}
name
— Placeholder identifier. In the console client it should be used in app parameters as--param_<name> = value
.data type
— Data type of the app parameter value. For example, a data structure like(integer, ('string', integer))
can have theTuple(UInt8, Tuple(String, UInt8))
data type (you can also use another integer types). It's also possible to pass table, database, column names as a parameter, in that case you would need to useIdentifier
as a data type.