offdataai
DuckDBSQLDDL

DuckDB CREATE SCHEMA: Syntax, Search Path and Attached Databases

How to create and use schemas in DuckDB, set the default schema and search path, work with schemas in attached database files, list and drop schemas, and organise a local warehouse.

ooffdataai2 min read

In DuckDB a schema is a namespace inside a database (a catalog). Every database starts with a schema called main, which is where tables go unless you say otherwise. You add more with CREATE SCHEMA.

CREATE SCHEMA syntax

sql
CREATE SCHEMA [ IF NOT EXISTS ] schema_name;
sql
CREATE SCHEMA IF NOT EXISTS staging;CREATE SCHEMA IF NOT EXISTS analytics;​CREATE TABLE analytics.dim_customer (  customer_key INTEGER PRIMARY KEY,  customer_id  VARCHAR NOT NULL UNIQUE,  name         VARCHAR,  region       VARCHAR);​CREATE TABLE analytics.fct_orders (  order_id     VARCHAR PRIMARY KEY,  customer_key INTEGER NOT NULL REFERENCES analytics.dim_customer (customer_key),  order_date   DATE NOT NULL,  net_amount   DECIMAL(18, 2) NOT NULL);

Unlike most cloud warehouses, DuckDB enforces primary key, unique and foreign key constraints, so a load that breaks them fails. That is useful for testing a model, though constraints slow down large bulk loads.

Set the default schema

sql
SET schema = 'analytics';                 -- unqualified names now resolve to analyticsSET search_path = 'analytics,staging';    -- look in several schemas, in orderUSE my_database.analytics;                -- choose database and schema together

Schemas in attached databases

DuckDB can attach several database files at once. Each has its own schemas, addressed with three-part names:

sql
ATTACH 'warehouse.duckdb' AS wh;CREATE SCHEMA IF NOT EXISTS wh.analytics;CREATE TABLE wh.analytics.dim_date ASSELECT d::DATE AS date_dayFROM range(DATE '2024-01-01', DATE '2027-01-01', INTERVAL 1 DAY) t(d);

List and drop schemas

sql
SELECT catalog_name, schema_name FROM information_schema.schemata;SELECT * FROM duckdb_schemas();​DROP SCHEMA IF EXISTS staging;           -- fails if it contains objectsDROP SCHEMA IF EXISTS staging CASCADE;   -- drops its tables and views too

Organising a local warehouse in DuckDB

DuckDB works well as a local or embedded warehouse, and with dbt-duckdb as a full analytics stack on a laptop. A tidy layout mirrors a cloud warehouse: raw for loaded files, staging for cleaned models, analytics for star schemas. Moving to Snowflake or BigQuery later is then mostly a change of dialect.

Frequently asked questions

What is the default schema in DuckDB?

main. Tables created without a schema name go there.

How do I switch schema in DuckDB?

SET schema = 'name'; or USE database.schema;.

Does DuckDB enforce foreign keys?

Yes. DuckDB checks primary key, unique and foreign key constraints when data changes.

Generate DuckDB DDL from a description

offdataai generates DuckDB DDL and a dbt-duckdb project from a plain-English description or one of 174 templates, so you can prototype a warehouse locally. See the DuckDB schema generator.

Try offdataai

Ship a warehouse from a single conversation.

Describe your domain. Get ERDs, DDL, dbt projects, and seed data for every major cloud warehouse.

Start building

Keep reading

All posts →