ClickHouse is a column-oriented database management system developed by ClickHouse, Inc. and written in C . It’s an Open Source OLAP database management system that is fast, fault-tolerant, easy to use, and highly reliable.

ClickHouse is a blazingly fast OLDAP database system for online analytical processing. It allows you to generate analytical reports using SQL queries in real-time.

In this tutorial, you will learn how to install the ClickHouse OLAP Database System on an Ubuntu 22.04 server. You’ll also learn how to set up authentication on ClickHouse and cover its basic usage and queries.

Prerequisites

To complete this tutorial, you’ll several prerequisites below:

  • An Ubuntu 22.04 server – This example uses an Ubuntu system with the hostname ‘clickhouse-server’ and the IP address ‘192.168.5.100’.
  • A non-root user with sudo/root administrator privileges.

Setting up System

Before starting the ClickHouse installation, you will ensure that the current CPU supports SSE 4.2 instruction set and disable SWAP on your system.

The official pre-built binary package for ClickHouse is compiled on the x86 (64-bit) architecture and the SSE 4.2. So, if you don’t have a CPU that supports SSE 4.2, you should build ClickHouse manually from the source.

Also, when using ClickHouse, it’s recommended to disable SWAP, especially in the production environment.

Run the below command to check that your CPU supports SSE 4.2 feature. You should receive the output ‘SSE 4.2 supported’ if your CPU is supported.

grep -q sse4_2 /proc/cpuinfo && echo "SSE 4.2 supported" || echo "SSE 4.2 not supported"

Next, run the below command to disable SWAP via the configuration ‘/etc/fstab‘ file. This command will put ‘#’ at the beginning of the SWAP configuration and disable SWAP in your system permanently.

sudo sed -i '/ swap / s/^(.*)$/#1/g' /etc/fstab

Now run the below command to turn off SWAP on the current session. Then verify the SWAP status.

sudo swapoff -a

sudo free -m

Below is the output you’ll receive if the SWAP is disabled – The SWAP size on the ‘total’ field is 0.

<img alt="check environment" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/04/echo/1-check-environment.png66103a1b959e5.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="176" loading="lazy" src="data:image/svg xml,” width=”750″>

With the CPU that supports SSE 4.2 and WAP is disabled, you’ll now ready to install ClickHouse OLAP Database System.

Installing Clickhouse OLAP Database System

ClickHouse OLDAP Database System provides multiple installation methods. For Linux distributions, ClcikHouse provides repositories for both Debian-based and RPM-based distributions. Also, ClickHouse is available with installation via Docker or using a single binary package.

In this step, you will install the ClickHouse on an Ubuntu 22.04 system via the official repository. So, you’ll add the ClickHouse repository to your system, then install the ClickHouse package via APT.

To start, run the apt command below to install some basic dependencies. When prompted, input y and press ENTER.

sudo apt install apt-transport-https ca-certificates dirmngr

<img alt="instal dependencies" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/04/echo/2-install-dependencies.png66103a1bc6473.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="305" loading="lazy" src="data:image/svg xml,” width=”750″>

Now run the below command to add the GPG key and the ClickHouse repository to your system. In this example, you will install ClickHouse from the stable branch of the repository.

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 8919F6BD2B48D754

echo "deb https://packages.clickhouse.com/deb stable main" | sudo tee

    /etc/apt/sources.list.d/clickhouse.list

<img alt="setup repo" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/04/echo/3-setup-repo.png66103a1c15868.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="176" loading="lazy" src="data:image/svg xml,” width=”750″>

Next, update and refresh your package index via the apt command below.

sudo apt update

You’ll receive that the ClickHouse repository is added.

<img alt="update repo" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/04/echo/4-update-repo.png66103a1c305ff.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="206" loading="lazy" src="data:image/svg xml,” width=”750″>

Now install the ‘clickhouse-server‘ as the database system and the ‘clickhouse-client‘ package via the apt command below.

sudo apt install clickhouse-server clickhouse-client

Input Y when prompted for the confirmation and press ENTER to proceed.

<img alt="install clickhouse server" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/04/echo/5-install-clickhouse-server-client.png66103a1c75765.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="311" loading="lazy" src="data:image/svg xml,” width=”750″>

Also, during the CLickHouse installation, you’ll be prompted to set up a password for the ClickHouse default user. Input a new password for ClickHouse and you’ll receive the output like the following screenshot when installation is finished.

<img alt="setup default password" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/04/echo/6-setup-default-password.png66103a1cadb8a.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="290" loading="lazy" src="data:image/svg xml,” width=”750″>

With the ClickHouse installed, run the below systemctl command utility to start and enable the ClickHouse service. This command will start the ClickHouse and enable the service to run automatically upon the bootup.

sudo systemctl start clickhouse-server

sudo systemctl enable clickhouse-server

Verify the ClicHouse service with the below command to ensure that the ClickHouse service is running.

sudo systemctl status clickhouse-server

You’ll receive the output like the following – The current status of the ClickHpouse service is running and it’s enabled and will be run automatically at startup.

<img alt="start verify ClickHouse" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/04/echo/7-start-enable-status-clickhouse.png66103a1cea94d.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="244" loading="lazy" src="data:image/svg xml,” width=”750″>

You have finished the ClickHouse server installation. In the next step, you will learn how to access the ClickHouse server via the client application and set up password authentication for the ClickHouse administrator user.

Accessing ClickHouse via Command Line

With the ClickHouse server installed, you can access ClickHouse via the ‘clickhouse-client‘ you’ve also installed.

The ‘clickhouse-client’ is a command-line interface for interacting and managing the ClickHouse server. It is similar to the ‘mysql‘ client for the MySQL/MariaDB server, ‘psql‘ for the PostgreSQL server or ‘mongod‘ for the MongoDB server.

To access the ClickHouse server, you can use the ‘clickhouse-client‘ command below.

sudo clickhouse-client --password

When prompted for the password, input the password that you created during the installation.

After logging in, you should see the ClickHouse shell as the following.

<img alt="connect to clickhouse" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/04/echo/8-connect-to-clickhouse.png66103a1d24688.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="283" loading="lazy" src="data:image/svg xml,” width=”714″>

From here, you can see that you’ve connected to the ClickHouse server v22.11 with the default ClickHouse user. The ClickHouse server is running by default on localhost with port 9000.

At this point, you installed the ClickHouse server and client and learned how to connect to the ClickHouse server via the command ‘clickhouse-client‘. In the next step, you’ll set up an administrator user for the ClickHouse server.

Setting up Administrator User on Clickhouse

In this step, you will create an administrator user for the ClickHouse server. But before that, you’ll enable the ‘Account Management’ for the ‘default’ user via the ClickHouse configuration.

To begin, open the configuration ‘/etc/clickhouse-server/users.d/default_password.xml’ using the below nano editor command.

sudo nano /etc/clickhouse-server/users.d/default_password.xml

Add the following line between the ‘…‘ section. This will enable the ‘Access Management‘ for the user ‘default‘.



   

       

            ...

            1

       

   

Save the file and exit the editor when you’re finished.

Now run the below systemctl command utility to restart the ClickHouse service and apply the changes.

sudo systemctl restart clickhouse-server

Now that the ‘access_management’ is enabled for the ‘default’ user, you’ll then create a new administrator user for ClickHouse. The new user will be able to access all databases with all privileges on the ClickHouse server.

Run the below ‘clickhouse-client‘ command to connect to the ClickHouse server via the user ‘default‘.

sudo clickhouse-client --user default --password

When prompted for the password, input the password that you have configured. Now you’ll get the ClickHouse server console and log in as the ‘default‘ user with ‘access_management‘ enabled.

<img alt="setup clickhouse" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/04/echo/10-restart-connect-clickhouse.png66103a1d3e624.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="250" loading="lazy" src="data:image/svg xml,” width=”750″>

Next, run the following query to create a new ClickHouse user. And be sure to change the username, password, and host IP address. This example will create a new user ‘halsey‘ with the password ‘password’ and hashed with SHA256. The ‘HOST IP‘ option allows you to specify the IP address that will be used to connect to the ClickHouse server. In this example, only allowed from localhost or ‘127.0.0.1‘.

CREATE USER halsey HOST IP '127.0.0.1' IDENTIFIED WITH sha256_password BY 'password';

<img alt="create a new user" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/04/echo/11-create-new-user.png66103a1d59af5.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="102" loading="lazy" src="data:image/svg xml,” width=”750″>

Now run the below query to grant all privileges on the ClickHouse server to the user ‘halsey‘.

GRANT ALL ON *.* TO halsey WITH GRANT OPTION;

<img alt="grant privileges" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/04/echo/12-grant-privileges.png66103a1d7480d.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="257" loading="lazy" src="data:image/svg xml,” width=”671″>

Next, verify the list of users on the ClickHouse server via the following query. You should see that the new user ‘halsey’ is added to the ClickHouse.

SHOW USERS;

<img alt="show users" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/04/echo/13-list-users.png66103a1d985b1.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="328" loading="lazy" src="data:image/svg xml,” width=”589″>

Now type ‘quit’ on the ClickHouse console client to exit.

With the new database admin user created, you’ll then must disable the ‘access_management’ for the ‘default‘ user on your ClickHouse server.

Open the configuration ‘/etc/clickhouse-server/users.d/default_password.xml‘ using the below nano editor command.

sudo nano /etc/clickhouse-server/users.d/default_password.xml

Change the ‘access_management‘ option to ‘0‘ to disable the ‘Access Management’ for the ‘default‘ user.



   

       

            ...

            0

       

   

Save the file and exit the editor when you’re finished.

Restart the ClickHouse service to apply the changes via the systemctl command utility below.

sudo systemctl restart clickhouse-server

Lastly, run the following command to log in to the ClickHouse server as the user ‘halsey’. When prompted for the password, input your password and press ENTER to log in.

sudo clickhouse-client --user halsey --password

After logging in, you’ll receive the output like the following screenshot. The new admin user for ClickHouse ‘halsey’ is added and the login is successful.

<img alt="connect as user" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/04/echo/15-connect-as-user.png66103a1db50cd.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="260" loading="lazy" src="data:image/svg xml,” width=”750″>

With the new admin user of the ClickHouse server created, you’ll next learn how to use ClickHouse to create databases and tables and insert and retrieve data via the ClickHouse SQL query.

Basic Queries on ClickHouse Database System

In this step, you will learn the basic query of the ClickHouse Database System, which is similar to common SQL queries. You’ll learn how to create a database and table, insert and retrieve data from the database, then the basic usage of the ‘ALTER‘ query that you can use to change existing data on the ClickHouse server.

First, run the following query to create a new database. In this example, you’ll create a new database ‘testdb‘.

CREATE DATABASE testdb;

<img alt="create database" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/04/echo/16-create-db.png66103a1de7589.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="252" loading="lazy" src="data:image/svg xml,” width=”489″>

Switch to the ‘testdb‘ via the following query ‘USE dbname’.

USE testdb;

<img alt="use database" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/04/echo/17-use-db.png66103a1e24f26.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="226" loading="lazy" src="data:image/svg xml,” width=”562″>

Now, create a new table, ‘users,’ with the following query. The table ‘users‘ will have some fields below:

  • id with type ‘UInt64’ for storing integer values.
  • name with type ‘String’  for storing string – length of character.
  • last_login with type ‘DateTime’ for string date time format data.
  • The engine here is ‘MergeTree’, mostly used on ClickHouse.
CREATE TABLE users (id UInt64, name String, jobs String, last_login DateTime) ENGINE=MergeTree() PRIMARY KEY id ORDER BY id;

<img alt="create table" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/04/echo/18-create-table.png66103a1e4734e.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="233" loading="lazy" src="data:image/svg xml,” width=”750″>

Next, run the following query to insert new data to the table ‘users’. In the below query, you’ll insert three different data to the table ‘users‘.

INSERT INTO users VALUES (1, 'alice', 'DevOps', '2022-10-10 00:10:10');

INSERT INTO users VALUES (2, 'alex', 'Manager', '2022-09-05 01:19:10');

INSERT INTO users VALUES (3, 'janet', 'Developer', '2022-05-05 04:29:10');

Insert the first data.

<img alt="insert data 1" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/04/echo/19-insert-data1.png66103a1e8b066.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="199" loading="lazy" src="data:image/svg xml,” width=”750″>

Insert second data.

<img alt="insert data 2" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/04/echo/20-insert-data2.png66103a1ea7309.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="190" loading="lazy" src="data:image/svg xml,” width=”750″>

Insert third data.

<img alt="insert data 3" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/04/echo/21-insert-data3.png66103a1ec24dc.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="186" loading="lazy" src="data:image/svg xml,” width=”750″>

Run the following query to retrieve data from the table ‘users‘. You should see three new data fields that you just added.

SELECT * FROM users;

<img alt="retrive data" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/04/echo/22-list-data.png66103a1edac77.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="396" loading="lazy" src="data:image/svg xml,” width=”499″>

You have learned how to create the database, switch to the database, create the table, insert data, and retrieve data on the ClickHouse OLAP Database System. Next, you’ll learn to change data with the ‘ALTER‘ query.

Run the following ‘ALTER TABLE’ query to update existing data on the table ‘users’. In this example, you’ll change the field ‘jobs’ for the user ‘alex‘ to ‘administrator‘.

ALTER TABLE users UPDATE jobs = 'Administrator' WHERE name = 'alex';

Verify the newly updated data on the table ‘users’ via the following query. You should now get the new data for the user ‘alex‘ updated to ‘Administrator‘.

SELECT * FROM users;

<img alt="Change data with ALTER" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/04/echo/23-change-alter-data.png66103a1f04d9b.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="468" loading="lazy" src="data:image/svg xml,” width=”750″>

Now that you’ve learned the basic usage of ClickHouse query for creating and switching databases, creating tables, inserting data, and retrieving data. And also, you have learned how to use the ‘ALTER‘ query to change existing data on the ClickHouse database. You’ll next clean up your ClickHouse installation.

Clean-up Environment

With the new database and table created and random data inserted, in this last step, you’ll clean up the ClickHouse Database System installation by deleting the database and table that you just created.

Run the following query, ‘DROP TABLE,’ to remove the users table from the testdb database.

DROP TABLE users;

<img alt="drop table" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/04/echo/24-drop-table.png66103a1f1ede4.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="217" loading="lazy" src="data:image/svg xml,” width=”531″>

Now remove the database ‘testdb’ via the ‘DROP DATABASE‘ query below.

DROP DATABASE testdb;

<img alt="drop database" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/04/echo/25-drop-database.png66103a1f4d102.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="220" loading="lazy" src="data:image/svg xml,” width=”517″>

Lastly, verify the list of databases on the ClickHouse server via the following query. You should see that the database ‘testdb‘ is deleted from the ClickHouse server.

SHOW DATABASES;

<img alt="verify database" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/04/echo/26-check-databases.png66103a1f722a4.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="330" loading="lazy" src="data:image/svg xml,” width=”496″>

Now your ClickHouse installation is cleaned with the admin user also configured.

Conclusion

Congratulations! You’ve finished the ClickHouse OLAP Database System installation and learned how to set up an administrator user on CLickHouse. And lastly, you’ve also learned the basic query for creating a database and table, inserting and retrieving data, changing existing data, and cleaning up the database and table.