Skip to main content

CREATE DATABASE

The CREATE DATABASE statement creates a new database in MotherDuck. It can be used to either copy entire databases into MotherDuck from your local environment, or to zero-copy clone databases inside of MotherDuck.

CREATE DATABASE <database_name> FROM enables you to:

  • create a remote database (on MotherDuck) from a local database.
  • create a remote database (on MotherDuck) from a remote database.. This is a metadata-only operation that copies no data.

Syntax

This SQL query creates a new database in MotherDuck.

CREATE [ OR REPLACE ] DATABASE [ IF NOT EXISTS ] <database name> [ FROM CURRENT_DATABASE() | FROM <database name> | FROM '<local/file/path.db>' | FROM "md:_share/.../" ];

You can also pass the name of an attached share or a share url as database name, for example CREATE DATABASE FROM my_share or CREATE DATABASE FROM "md:_share/...".

If you attempt to create a database, yet a database with that name already exists, no new database will be created and the query will return an error. The error will be silenced when you specify IF NOT EXISTS.

Similar to the DuckDB table name conventions, database names that start with a number or contain special characters must be double quoted when used. Example: CREATE DATABASE "123db"

Creating a database does not automatically make it your current database: execute the SQL command USE DATABASE <database name> to do so.

Example usage

CREATE OR REPLACE DATABASE ducks;     -- if ducks database exists, even if populated, it will be replaced with an empty one
CREATE DATABASE IF NOT EXISTS ducks_db; -- if ducks_db database exists, the operation will be skipped, but will not error

To copy an entire database from your local DuckDB instance into MotherDuck:

USE ducks_db;
CREATE DATABASE ducks FROM CURRENT_DATABASE();
CREATE OR REPLACE DATABASE ducks FROM ducks_db; -- alternative syntax

To zero-copy clone a database in MotherDuck

CREATE DATABASE cloud_db FROM another_cloud_db;

To upload a local DuckDB database file

CREATE DATABASE flying_ducks FROM './databases/local_ducks.db';

To upload an attached local DuckDB database

ATTACH './databases/local_ducks.db';
CREATE DATABASE flying_ducks FROM local_ducks;