Attach Database
Attach and read data across multiple databases.
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
- You enable the
ATTACH
feature on the databases you want to connect to. - You retrieve the Database ID for the database you want to
ATTACH
. - 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.
- CLI:
- 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 installed, and logged in.
Enable attach on required databases
You will first need to enable the ATTACH
feature on the database(s) you want to attach:
Retrieve Database ID
You now need to retrieve the Database ID for the database you want to ATTACH
:
Connect to any database with attach
Now pass the names of the databases via the --attach
flag when connecting to your database(s):
ATTACH
Now once connected to the database you can invoke an ATTACH
statement to connect the other database(s):
Query
Execute a query using the alias for any attached database(s):
libSQL SDKs
You can use one of the libSQL client SDKs with TypeScript, Rust, Go, Python, or over HTTP.
Enable attach on required databases
You will first need to enable the ATTACH
feature on the database(s) you want to attach:
Retrieve Database ID
You now need to retrieve the Database ID for the database you want to ATTACH
:
Create token with ATTACH permissions
Now create a token for the libSQL client with the attach
permission for the database you want to attach:
Connect to any database with ATTACH
Use a Client SDK to attach the desired database within a read transaction:
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
Was this page helpful?