Install
To download ClickHouse, run:Run
If you only downloaded but did not install ClickHouse, use
./clickhouse client instead of clickhouse-client.
For a complete list of command-line options, see Command Line Options.
Connecting to ClickHouse Cloud
The details for your ClickHouse Cloud service are available in the ClickHouse Cloud console. Select the service that you want to connect to and click Connect:Choose Native, and the details are shown with an example
clickhouse-client command:
Storing connections in a configuration file
You can store connection details for one or more ClickHouse servers in a configuration file. The format looks like this:To concentrate on the query syntax, the rest of the examples leave off the connection details (
--host, --port, etc.). Remember to add them when you use the commands.Interactive mode
Using interactive mode
To run ClickHouse in interactive mode, simply execute:PrettyCompact.
You can change the format in the FORMAT clause of the query or by specifying the --format command-line option.
To use the Vertical format, you can use --vertical or specify \G at the end of the query.
In this format, each value is printed on a separate line, which is convenient for wide tables.
In interactive mode, by default whatever was entered is run when you press Enter.
A semicolon is not necessary at the end of the query.
You can start the client with the -m, --multiline parameter.
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.
To run the query, end it with a semicolon and press Enter.
ClickHouse Client is based on replxx (similar to readline) so it uses familiar keyboard shortcuts and keeps a history.
The history is written to ~/.clickhouse-client-history by default.
To exit the client, press Ctrl+D, or enter one of the following instead of a query:
exitorexit;quitorquit;q,Qor:qlogoutorlogout;
Getting help
You can look up the documentation of any function, table engine, data type, format, setting and other component of the system without leaving the client. Enterhelp followed by a name (the equivalent forms /help, man and /man also work):
system.documentation table. The matching documentation is rendered from Markdown in the terminal, with bold/italic text, tables, and syntax-highlighted code blocks. When a name is shared by several components (for example file, which is both a function and a table engine), all of them are shown.
When nothing matches exactly, the client lists similar names (allowing for typos) and the components whose documentation mentions the word:
help on its own prints a short usage summary.
Query processing information
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.
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.
ClickHouse Client allows passing external data (external temporary tables) for querying.
For more information, see the section External data for query processing.
Aliases
You can use the following aliases from within the REPL:\l- SHOW DATABASES\d- SHOW TABLES\c <DATABASE>- USE DATABASE.- repeat the last query
Keyboard shortcuts
Alt (Option) + Shift + e- open editor with the current query. It is possible to specify the editor to use with the environment variableEDITOR. By default,vimis used.Alt (Option) + #- comment line.Ctrl + r- fuzzy history search.
Batch mode
Using batch mode
Instead of using ClickHouse Client interactively, you can run it in batch mode. In batch mode, ClickHouse executes a single query and exits immediately - there’s no interactive prompt or loop. You can specify a single query like this:--query command-line option:
stdin:
messages, you can also insert data from the command line:
--query is specified, any input is appended to the request after a line feed.
Inserting a CSV file into a remote ClickHouse service
This example is inserting a sample dataset CSV file,cell_towers.csv into an existing table cell_towers in the default database:
Examples of inserting data from the command line
There are several ways to insert data from the command line. The example below inserts two rows of CSV data into a ClickHouse table using batch mode:cat <<_EOF starts a heredoc that will read everything until it sees _EOF again, then outputs it:
cat, and piped into clickhouse-client as input:
TabSeparated.
You can set the format in the FORMAT clause of the query as shown in the example above.
Queries with parameters
You can specify parameters in a query and pass values to it with command-line options. This avoids formatting a query with specific dynamic values on the client side. For example:Query syntax
In the query, place the values that you want to fill using command-line parameters in braces in the following format:Examples
AI-powered SQL generation
ClickHouse Client includes built-in AI assistance for generating SQL queries from natural language descriptions. This feature helps users write complex queries without deep SQL knowledge. The AI assistance works out of the box if you have eitherOPENAI_API_KEY or ANTHROPIC_API_KEY environment variable set. For more advanced configuration, see the Configuration section.
Usage
To use AI SQL generation, prefix your natural language query with??:
- Explore your database schema automatically
- Generate appropriate SQL based on the discovered tables and columns
- Execute the generated query immediately
Example
Configuration
AI SQL generation requires configuring an AI provider in your ClickHouse Client configuration file. You can use either OpenAI, Anthropic, or any OpenAI-compatible API service.Environment-based fallback
If no AI configuration is specified in the config file, ClickHouse Client will automatically try to use environment variables:- First checks for
OPENAI_API_KEYenvironment variable - If not found, checks for
ANTHROPIC_API_KEYenvironment variable - If neither is found, AI features will be disabled
Configuration file
For more control over AI settings, configure them in your ClickHouse Client configuration file located at:$XDG_CONFIG_HOME/clickhouse/config.xml(or~/.config/clickhouse/config.xmlifXDG_CONFIG_HOMEis not set) (XML format)$XDG_CONFIG_HOME/clickhouse/config.yaml(or~/.config/clickhouse/config.yamlifXDG_CONFIG_HOMEis not set) (YAML format)~/.clickhouse-client/config.xml(XML format, legacy location)~/.clickhouse-client/config.yaml(YAML format, legacy location)- Or specify a custom location with
--config-file
- XML
- YAML
Using OpenAI-compatible APIs (e.g., OpenRouter):
Parameters
Required parameters
Required parameters
api_key- Your API key for the AI service. Can be omitted if set via environment variable:- OpenAI:
OPENAI_API_KEY - Anthropic:
ANTHROPIC_API_KEY - Note: API key in config file takes precedence over environment variable
- OpenAI:
provider- The AI provider:openaioranthropic- If omitted, uses automatic fallback based on available environment variables
Model configuration
Model configuration
model- The model to use (default: provider-specific)- OpenAI:
gpt-4o,gpt-4,gpt-3.5-turbo, etc. - Anthropic:
claude-3-5-sonnet-20241022,claude-3-opus-20240229, etc. - OpenRouter: Use their model naming like
anthropic/claude-3.5-sonnet
- OpenAI:
Connection settings
Connection settings
base_url- Custom API endpoint for OpenAI-compatible services (optional)timeout_seconds- Request timeout in seconds (default:30)
Schema exploration
Schema exploration
enable_schema_access- Allow AI to explore database schemas (default:true)max_steps- Maximum tool-calling steps for schema exploration (default:10)
Generation parameters
Generation parameters
temperature- Controls randomness, 0.0 = deterministic, 1.0 = creative. Omitted by default and only sent to the model when explicitly set, because some models reject this parameter.max_tokens- Maximum response length in tokens (default:1000)system_prompt- Custom instructions for the AI (optional)
How it works
The AI SQL generator uses a multi-step process:- Schema Discovery
- Lists available databases
- Discovers tables within relevant databases
- Examines table structures via
CREATE TABLEstatements
- Query Generation
- Matches your natural language intent
- Uses correct table and column names
- Applies appropriate joins and aggregations
- Execution
Limitations
- Requires an active internet connection
- API usage is subject to rate limits and costs from the AI provider
- Complex queries may require multiple refinements
- The AI has read-only access to schema information, not actual data
Security
- API keys are never sent to ClickHouse servers
- The AI only sees schema information (table/column names and types), not actual data
- All generated queries respect your existing database permissions
Connection string
Usage
ClickHouse Client alternatively supports connecting to a ClickHouse server using a connection string similar to MongoDB, PostgreSQL, MySQL. It has the following syntax:Notes
If the username, password or database was specified in the connection string, it cannot be specified using--user, --password or --database (and vice versa).
The host component can either be a hostname or an IPv4 or IPv6 address.
IPv6 addresses should be in []:
clickHouse-client.
The connection string can be combined with an arbitrary number of other command-line options except --host and --port.
The following keys are allowed for query_parameters:
Percent encoding
Non-US ASCII, spaces and special characters in the following parameters must be percent-encoded:
userpasswordhostsdatabasequery parameters
Examples
Connect tolocalhost on port 9000 and execute the query SELECT 1.
localhost as user john with password secret, host 127.0.0.1 and port 9000
localhost as the default user, host with IPV6 address [::1] and port 9000.
localhost on port 9000 in multiline mode.
localhost using port 9000 as the user default.
localhost on port 9000 and default to the my_database database.
localhost on port 9000 and default to the my_database database specified in the connection string and a secure connection using the shorthanded s parameter.
my_user and no password.
localhost using the email as the user name. @ symbol is percent encoded to %40.
192.168.1.15, 192.168.1.25.
Query ID format
In interactive mode ClickHouse Client shows the query ID for every query. By default, the ID is formatted like this:query_id_formats tag. The {query_id} placeholder in the format string is replaced with the query ID. Several format strings are allowed inside the tag.
This feature can be used to generate URLs to facilitate profiling of queries.
Example
Configuration files
ClickHouse Client uses the first existing file of the following:- A file that is defined with the
-c [ -C, --config, --config-file ]parameter. ./clickhouse-client.[xml|yaml|yml]$XDG_CONFIG_HOME/clickhouse/config.[xml|yaml|yml](or~/.config/clickhouse/config.[xml|yaml|yml]ifXDG_CONFIG_HOMEis not set)~/.clickhouse-client/config.[xml|yaml|yml]/etc/clickhouse-client/config.[xml|yaml|yml]
clickhouse-client.xml
- XML
- YAML
Environment variable options
The user name, password and host can be set via environment variablesCLICKHOUSE_USER, CLICKHOUSE_PASSWORD and CLICKHOUSE_HOST.
Command line arguments --user, --password or --host, or a connection string (if specified) take precedence over environment variables.
Command-line options
All command-line options can be specified directly on the command line or as defaults in the configuration file.General options
Connection options
Instead of the
--host, --port, --user and --password options, the client also supports connection strings.