# Attach Database (Deprecated)

> Attach and read data across multiple databases.
> Canonical HTML: https://tursodb.cn/docs/features/attach-database/
> Upstream Source (MIT): https://docs.turso.tech/features/attach-database
> Language: en

---
<div class="doc-callout doc-callout-warning"><div class="doc-callout-title">WARNING</div><div class="doc-callout-body">

This feature is now deprecated for all new users. Existing paid users can
continue to use `ATTACH` — [read the
announcement](https://turso.tech/blog/upcoming-changes-to-the-turso-platform-and-roadmap)

</div></div>


The `ATTACH` statement enables you to link multiple databases within a single transaction, which is ideal for:

* Organizing data in a modular way
* Streamlining data access and enhancing scalability
* Aggregating data

## How it works

1. You enable the `ATTACH` feature on the databases you want to connect to.
2. You retrieve the **Database ID** for the database you want to `ATTACH`.
3. You connect to the database
   * **CLI**: `--attach` flag to automatically create a token with the correct permissions.
   * **SDK**: Create a token with the `attach` permission for the database you want to attach.
4. You invoke `ATTACH` to connect to the other databases within the database shell or SDK.

## Usage

You can use the `ATTACH` statement to connect to other databases within a transaction using the CLI, or libSQL SDK. Once attached, you can query the attached databases as if they were part of the current database using the assigned alias.

### Turso CLI

Make sure you have the [Turso CLI](/docs/cli/installation/) installed, and [logged in](/docs/cli/auth/login/).

**1. Enable attach on required databases**

You will first need to enable the `ATTACH` feature on the database(s) you want to attach:

```bash
turso db config attach allow <database-name>
```

**2. Retrieve Database ID**

You now need to retrieve the **Database ID** for the database you want to `ATTACH`:

```bash
turso db show <database-name>
```

**3. Connect to any database with attach**

Now pass the names of the databases via the `--attach` flag when connecting to your database(s):

```bash
turso db shell <database-name> --attach <...database-name(s)>
```

**4. ATTACH**

Now once connected to the database you can invoke an `ATTACH` statement to connect the other database(s):

```sql
ATTACH "<database-id>" AS my_db;
```

**5. Query**

Execute a query using the alias for any attached database(s):

```sql
SELECT * FROM my_db.my_table;
```

### libSQL SDKs

You can use one of the libSQL client SDKs with [TypeScript](https://docs.turso.tech/sdk/ts), [Rust](https://docs.turso.tech/sdk/rust), [Go](https://docs.turso.tech/sdk/go), [Python](https://docs.turso.tech/sdk/python), or over [HTTP](https://docs.turso.tech/sdk/http).

**1. Enable attach on required databases**

You will first need to enable the `ATTACH` feature on the database(s) you want to attach:

```bash
turso db config attach allow <database-name>
```

**2. Retrieve Database ID**

You now need to retrieve the **Database ID** for the database you want to `ATTACH`:

```bash
turso db show <database-name>
```

**3. Create token with ATTACH permissions**

Now create a token for the libSQL client with the `attach` permission for the database you want to attach:

```bash
turso db tokens create <database-name> --attach <another-database-name>
```

**4. Connect to any database with ATTACH**

Use a [Client SDK](https://docs.turso.tech/sdk) to attach the desired database within a read transaction:

```ts @libsql/client
import { createClient } from "@libsql/client";

const client = createClient({
  syncUrl: "libsql://...",
  authToken: "...",
});

const txn = await db.transaction("read");

await txn.execute('ATTACH "<database-id>" AS my_db');

const rs = await txn.execute("SELECT * FROM my_db.my_table");
```

## Things to know

* You can only attach databases that have the `attach` feature enabled.
* You can only attach databases belonging to a group, and in the same group.
* There is a maximum of 10 databases that can be attached to a single transaction.
* The attached databases are read only.
* `ATTACH` statement can be used only within transactions.
* `ATTACH` doesn't support [Embedded Replicas](https://docs.turso.tech/features/embedded-replicas)
