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. To attach to a MotherDuck database, the md: prefix is used.

Syntax

ATTACH 'md:<database>'

Parameters:

  • database: The name of the database to which to connect. If omitted, it defaults to 'workspace', which connects to all databases.

Example of usage

-- Connect to a single specific database:
ATTACH 'md:stocks_dev';

-- Connect to all databases in the workspace:
ATTACH 'md:';

-- Connect to a local database
ATTACH '/path/to/my_database.duckdb';
ATTACH 'a_new_local_duckdb';

Notes

  • 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.
  • 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.
  • 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.
  • 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.