Skip to main content

ATTACH <database>

A local database can be attached in order to access local data, and a remote MotherDuck database that a user has created and has previously detached may be re-attached.

In the case of a local database, the attach operation is temporary and will only last as long as the connection, and the data is not automatically uploaded to MotherDuck upon attach. For a remote MotherDuck database, the attach operation is persistent.

Note that the syntax is slightly different from attaching a database share. For a local database, the path is used rather than the share URL. For a remote MotherDuck database, the database name is used to indicate what to attach and no alias/renaming is permitted. In the case of a remote MotherDuck database, the database must have been created by the active user and must have already been detached.

Similarly to a share, the database path or name must be wrapped in single quotes, as it can be a new name that MotherDuck has not seen before. Note that DETACH <database name> requires that no single quotes be used (since the database is a valid object identifier at that point).

If a remote database that has been detached exists, that database will take precedence and will be used rather than a local database. In the case that a local database exists with the same name as a remote MotherDuck database, the md: prefix may be used to specify the remote database. This is also a good safety measure. If the remote database was not detached prior to running the ATTACH command, using the md: prefix will produce an error rather than creating a local database and attaching it.

To see all databases, both attached and detached, use the SHOW ALL DATABASES command.

Syntax

ATTACH '<path to local DuckDB database>';

ATTACH '<remote MotherDuck database name>';

ATTACH 'md:<remote MotherDuck database name>';

Example usage

ATTACH '/path/to/my_database.duckdb';
ATTACH 'a_new_local_duckdb';

-- Prior commands:
-- CREATE DATABASE my_md_database
-- DETACH my_md_database
ATTACH 'my_md_database';
-- OR
ATTACH 'md:my_md_database';