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
CREATE SCHEMA [ IF NOT EXISTS ] schema_name;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
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 togetherSchemas in attached databases
DuckDB can attach several database files at once. Each has its own schemas, addressed with three-part names:
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
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 tooOrganising 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.
