Querying a shared MotherDuck database
In order to use a database that a colleague shared with you, you can attach it using the DuckDB ATTACH
command. All that you need is a share URL from your colleague.
To detach a database, use the DETACH
command.
If the shared database you're reading from is updated (the owner uses the UPDATE SHARE command), you will automatically see raw data related changes (like inserts and updates) within 1 minute. The impact of ALTER TABLE
statements will also be reflected automatically. To see catalog changes (from things like CREATE TABLE/VIEW/SCHEMA
statements) in addition to raw data changes, you'll need to refresh the web page when using the UI. If you're using the CLI or a native client, you'll need to initiate a new connection.
In the following example, we will use the sample_data
database that is automatically attached when you connect to MotherDuck to illustrate how to use the ATTACH
and DETACH
command.
- CLI
Let's first DETACH
the sample_data
database as this one is automatically attached when you connect to MotherDuck.
Let's connect to MotherDuck and list the databases.
.open md:
show databases;
You should see the sample_data
which contains sample datasets to query. It's a shared database which is auto attached to any MotherDuck user.
┌─────────────────┐
│ database_name │
│ varchar │
├─────────────────┤
│ cloud_ducks │
│ my_db │
│ sample_data │
└─────────────────┘
Now, let's DETACH
the sample_data
database.
DETACH sample_data;
show databases;
The sample_data
database should not be listed anymore.
Let's attach the sample_data
database again.
Syntax
ATTACH '<share URL>' [AS <database name>];
If you omit the database name, the database will be named the same as the source database.
ATTACH 'md:_share/sample_data/23b0d623-1361-421d-ae77-62d701d471e6';
USE sample_data;
An attached shared database behaves just like a read-only database in every way.