Updated on 2024-04-29 GMT+08:00

CREATE DATABASE

This section describes the syntax of a CREATE DATABASE statement that is used to create a database in ClickHouse.

CREATE DATABASE Syntax

CREATE DATABASE [IF NOT EXISTS] db_name [ON CLUSTER ClickHouse cluster name];
Table 1 Parameter description

Parameter

Description

db_name

Database name.

IF NOT EXISTS

You can add the IF NOT EXISTS keyword phrase to a CREATE DATABASE statement. In this case, if the name specified in the statement has been used by an existing database, no database is created and no error is returned.

ON CLUSTER ClickHouse cluster name

This parameter specifies the name of a cluster.

You can run the following statement to obtain the cluster name from the cluster field:

select cluster,shard_num,replica_num,host_name from system.clusters;

Use Example

  • Create a database named demo.
    create database demo ON CLUSTER default_cluster;
  • View the created database.
    host-172-16-30-9 :) show databases;
    SHOW DATABASES
    Query id: ced1af23-0286-40cc-9c7a-ccbca41178d8
    ┌─name───────────────┐
    │ INFORMATION_SCHEMA │
    │ default            │
    │ demo               │
    │ information_schema │
    │ system             │
    └────────────────────┘
    5 rows in set. Elapsed: 0.002 sec.