COPY TABLE copies both schema and data from a source database to a target database in ClickHouse.
Moving a table lets you reorganize data, split workloads, or migrate to a new cluster without downtime. ClickHouse supports fast, online copies that preserve table engines, settings, and data.
Three approaches dominate: 1) CREATE TABLE ... AS, 2) INSERT INTO ... SELECT, and 3) BACKUP / RESTORE.Choose based on size, engine, and need for exact metadata.
Use CREATE TABLE db2.Customers AS db1.Customers ENGINE = MergeTree().It clones structure and then streams data.
Run INSERT INTO db2.Customers SELECT * FROM db1.Customers; This pushes data through the cluster and respects partitions.
BACKUP TABLE db1.Customers TO '/var/backup/' and RESTORE TABLE db1.Customers FROM '/var/backup/' AS db2.Customers; handles terabytes atomically and is resumable.
Use the clickhouse-copier tool for cross-cluster transfers.It distributes tasks, retries automatically, and maintains replication state.
Copy reference tables (Products) first, then fact tables (Orders, OrderItems) to maintain foreign-key logic at the application layer. Validate row counts and sample checksums.
Compare counts: SELECT count() FROM db1.Orders; and SELECT count() FROM db2.Orders; Differences signal missing partitions or failed shards.
Create a MATERIALIZED VIEW on db1.Orders that INSERTs into db2.Orders for continuous replication until cutover.
.
No. It reads parts without exclusive locks, so production queries continue unaffected.
Yes, but BACKUP/RESTORE requires both servers to be ≥20.3. For mixed versions, use INSERT SELECT or clickhouse-copier.
Set max_threads, max_insert_block_size, and use FINAL only if needed. Copy by partition for incremental loads.