CREATE DATABASE
Creates a new database.
Clauses
IF NOT EXISTS
If the db_name database already exists, then ClickHouse does not create a new database and:
- Doesn't throw an exception if clause is specified.
- Throws an exception if clause isn't specified.
ON CLUSTER
ClickHouse creates the db_name database on all the servers of a specified cluster. More details in a Distributed DDL article.
ENGINE
By default, ClickHouse uses its own Atomic database engine. There are also MySQL, PostgresSQL, MaterializedPostgreSQL, Replicated, SQLite.
COMMENT
You can add a comment to the database when you are creating it.
The comment is supported for all database engines.
Syntax
Example
Query:
Result:
SETTINGS
lazy_load_tables
When enabled, tables are not fully loaded during database startup. Instead, a lightweight proxy is created for each table and the real table engine is materialized on first access. This reduces startup time and memory usage for databases with many tables where only a subset is actively queried.
Applies to database engines that store table metadata on disk (e.g. Atomic, Ordinary). Views, materialized views, dictionaries, and tables backed by table functions are always loaded eagerly regardless of this setting.
When to use: This setting is useful for databases with a large number of tables (hundreds or thousands) where only a subset is actively queried. It reduces server startup time and memory usage by deferring the creation of table engine objects, scanning of data parts, and initialization of background threads until first access.
Impact on system.tables:
- Before a table is accessed,
system.tablesshows its engine asTableProxy. After first access, it shows the real engine name (e.g.MergeTree). - Columns like
total_rowsandtotal_bytesreturnNULLfor unloaded tables because the real storage has not been created yet.
Interaction with DDL operations:
SELECT,INSERT,ALTER,DROPtransparently trigger loading of the real table engine on first use.RENAME TABLEworks without triggering a load.- Once a table is loaded, it stays loaded for the lifetime of the server process.
Limitations:
- Monitoring tools that rely on
system.tablesmetadata (e.g.total_rows,engine) may see incomplete information for unloaded tables. - The first query to an unloaded table incurs a one-time loading cost (parsing the stored
CREATE TABLEstatement and initializing the engine).
Default value: 0 (disabled).