How to EXPORT SCHEMA in ClickHouse

Galaxy Glossary

How do I export only the schema (DDL) in ClickHouse?

EXPORT SCHEMA writes the DDL of one or more ClickHouse databases or tables to disk so you can version-control or migrate the structures without data.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Description

Table of Contents

What problem does EXPORT SCHEMA solve?

EXPORT SCHEMA lets you dump only the database objects—databases, tables, views, materialized views—so you can move structures between clusters or keep them in Git without transferring terabytes of data.

How do I export an entire schema?

Run EXPORT SCHEMA myshop_db TO '/var/ck_dumps/myshop_ddl/' FORMAT SQL;. ClickHouse creates one .sql file per object, each containing a CREATE statement that can be replayed on another server.

Can I export selected tables?

Yes.Separate multiple tables with commas: EXPORT SCHEMA myshop_db.Customers, myshop_db.Orders TO '/tmp/ecom_schema/' FORMAT SQL;.

How do I include table engines and TTLs?

Engine, partition keys, ordering, TTL, comments, and settings are always present in the generated DDL, guaranteeing a byte-for-byte recreation.

What formats are supported?

SQL is default; Native and JSONEachRow are available when you need programmatic parsing.Specify with FORMAT keyword.

Where does ClickHouse write the files?

ClickHouse must have write permission on the target path, which must reside on the same server where the query runs. Use an NFS mount for remote storage.

How do I re-import the schema?

Pipe the .sql files into clickhouse-client --multiquery or use psql-style \i commands inside the client. For Native/JSONEachRow, use IMPORT SCHEMA.

Best practice: version-control your DDL

Commit the generated .sql files to Git after every migration.Pair with CI to apply the DDL automatically in staging.

Best practice: automate with cron

Schedule nightly EXPORT SCHEMA jobs. Keep only the last N dumps to save disk space.

Common mistakes

Wrong path permissions: EXPORT fails silently when ClickHouse lacks write rights. Fix with chown/chmod.

Missing FORMAT keyword: Omitting FORMAT defaults to SQL. Explicitly set FORMAT Native when you need high-speed IMPORT.

Related commands

Use SHOW CREATE TABLE for a quick single-table DDL.Use BACKUP for both schema and data.

.

Why How to EXPORT SCHEMA in ClickHouse is important

How to EXPORT SCHEMA in ClickHouse Example Usage


-- Dump only the structural definitions of the order pipeline
EXPORT SCHEMA myshop_db.Customers,
              myshop_db.Orders,
              myshop_db.OrderItems
TO '/var/exports/order_pipeline/' FORMAT SQL;

How to EXPORT SCHEMA in ClickHouse Syntax


EXPORT SCHEMA [db_name | db_name.table_list]
TO 'destination_path/'
[FORMAT SQL | Native | JSONEachRow]
[SETTINGS output_format_write_statistics = 0];

-- Export the entire e-commerce schema
EXPORT SCHEMA myshop_db TO '/var/exports/myshop_schema/' FORMAT SQL;

-- Export only structural objects for three tables
EXPORT SCHEMA myshop_db.Customers, myshop_db.Orders, myshop_db.OrderItems 
TO '/var/exports/order_core/' FORMAT SQL;

Common Mistakes

Frequently Asked Questions (FAQs)

Does EXPORT SCHEMA lock tables?

No. ClickHouse reads system tables to build DDL, so no data locks occur and the command is fast.

Can I filter objects by pattern?

Not yet. List each table explicitly or export the full database and delete unwanted .sql files.

Is EXPORT SCHEMA idempotent?

Yes. Re-running the generated CREATE statements on an empty cluster reproduces the same structures.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.