> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-parallel-read-in-order-multi-part.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> Documentation for Attach

# ATTACH

Attaches a table or a dictionary, for example, when moving a database to another server.

**Syntax**

```sql theme={null}
ATTACH TABLE|DICTIONARY|DATABASE [IF NOT EXISTS] [db.]name [ON CLUSTER cluster] ...
```

The query does not create data on disk, but assumes that data is already in the appropriate places, and just adds information about the specified table, dictionary or database to the server. After executing the `ATTACH` query, the server will know about the existence of the table, dictionary or database.

If a table was previously detached ([DETACH](/reference/statements/detach) query), meaning that its structure is known, you can use shorthand without defining the structure.

<h2 id="attach-existing-table">
  Attach Existing Table
</h2>

**Syntax**

```sql theme={null}
ATTACH TABLE [IF NOT EXISTS] [db.]name [ON CLUSTER cluster]
```

This query is used when starting the server. The server stores table metadata as files with `ATTACH` queries, which it simply runs at launch (with the exception of some system tables, which are explicitly created on the server).

If the table was detached permanently, it won't be reattached at the server start, so you need to use `ATTACH` query explicitly.

<h2 id="create-new-table-and-attach-data">
  Create New Table And Attach Data
</h2>

<h3 id="with-specified-path-to-table-data">
  With Specified Path to Table Data
</h3>

The query creates a new table with provided structure and attaches table data from the provided directory in `user_files`.
The user needs the `READ ON FILE` and `WRITE ON FILE` privileges for this query: it reads the directory and moves it to the data path of the new table.

**Syntax**

```sql theme={null}
ATTACH TABLE name FROM 'path/to/data/' (col1 Type1, ...)
```

**Example**

```sql title="Query" theme={null}
DROP TABLE IF EXISTS test;
INSERT INTO TABLE FUNCTION file('01188_attach/test/data.TSV', 'TSV', 's String, n UInt8') VALUES ('test', 42);
ATTACH TABLE test FROM '01188_attach/test' (s String, n UInt8) ENGINE = File(TSV);
SELECT * FROM test;
```

```sql title="Response" theme={null}
┌─s────┬──n─┐
│ test │ 42 │
└──────┴────┘
```

<h3 id="with-specified-table-uuid">
  With Specified Table UUID
</h3>

This query creates a new table with provided structure and attaches data from the table with the specified UUID.
It is supported by the [Atomic](/reference/engines/database-engines/atomic) database engine.

**Syntax**

```sql theme={null}
ATTACH TABLE name UUID '<uuid>' (col1 Type1, ...)
```

<h2 id="attach-mergetree-table-as-replicatedmergetree">
  Attach MergeTree table as ReplicatedMergeTree
</h2>

Allows to attach non-replicated MergeTree table as ReplicatedMergeTree. ReplicatedMergeTree table will be created with values of `default_replica_path` and `default_replica_name` settings. It is also possible to attach a replicated table as a regular MergeTree.

The conversion is supported for tables in `Atomic` and `Ordinary` databases. A table in an `Ordinary` database has no UUID, so the conversion generates a UUID, expands `default_replica_path` with it once, and stores the resulting path explicitly in the engine arguments of the converted table. To add further replicas of such a table, specify this path explicitly in the first argument of the `ReplicatedMergeTree` engine; it can be found in the `zookeeper_path` column of `system.replicas`. Because the stored path no longer contains the `{uuid}` macro, the znode such a table owns is found by matching the path against `default_replica_path` again on every load; the conversion is refused when that template cannot be matched back (for example, when it expands `{uuid}` more than once). `{uuid}` in `default_replica_name` is not supported for any conversion.

Note that table's data in ZooKeeper is not affected in this query. This means you have to add metadata in ZooKeeper using `SYSTEM RESTORE REPLICA` or clear it with `SYSTEM DROP REPLICA ... FROM ZKPATH ...` after attach.

If you are trying to add a replica to an existing ReplicatedMergeTree table, keep in mind that all the local data in converted MergeTree table will be detached.

**Syntax**

```sql theme={null}
ATTACH TABLE [db.]name AS [NOT] REPLICATED
```

**Convert table to replicated**

```sql theme={null}
DETACH TABLE test;
ATTACH TABLE test AS REPLICATED;
SYSTEM RESTORE REPLICA test;
```

**Convert table to not replicated**

Get ZooKeeper path and replica name for table:

```sql title="Query" theme={null}
SELECT replica_name, zookeeper_path FROM system.replicas WHERE table='test';
```

```sql title="Response" theme={null}
┌─replica_name─┬─zookeeper_path─────────────────────────────────────────────┐
│ r1           │ /clickhouse/tables/401e6a1f-9bf2-41a3-a900-abb7e94dff98/s1 │
└──────────────┴────────────────────────────────────────────────────────────┘
```

Attach table as not replicated and delete replica's data from ZooKeeper:

```sql title="Query" theme={null}
DETACH TABLE test;
ATTACH TABLE test AS NOT REPLICATED;
SYSTEM DROP REPLICA 'r1' FROM ZKPATH '/clickhouse/tables/401e6a1f-9bf2-41a3-a900-abb7e94dff98/s1';
```

<h2 id="attach-existing-dictionary">
  Attach Existing Dictionary
</h2>

Attaches a previously detached dictionary.

**Syntax**

```sql theme={null}
ATTACH DICTIONARY [IF NOT EXISTS] [db.]name [ON CLUSTER cluster]
```

<h2 id="attach-existing-database">
  Attach Existing Database
</h2>

Attaches a previously detached database.

**Syntax**

```sql theme={null}
ATTACH DATABASE [IF NOT EXISTS] name [ENGINE=<database engine>] [ON CLUSTER cluster]
```
