# Attach a database as a catalog

Acquire / database

A variant of [Read from a database](https://meridian.online/operations/read-from-a-database).

Register a database, file or catalog so its tables can be queried in place by name.

Page: <https://meridian.online/operations/read-from-a-database/attach-a-database-as-a-catalog>

## Status

Runs as a SQL step.

arcform carries it as a `sql:` step today, with the `sqlite_scanner` and `ducklake` extensions.

## The DuckDB step

attach a SQLite database, a DuckDB file and a DuckLake catalog, then query their tables by name

It reads the `orders` and `customers` tables the proofs share. Create them first:

```sql
CREATE TABLE orders AS SELECT * FROM (VALUES
  (1,  'Alice Ng',      'north', DATE '2026-01-03', 120.00, 'widget', 'a|b',   '{"channel":"web","items":[1,2]}'),
  (2,  'alice ng',      'north', DATE '2026-01-05',  80.50, 'gadget', 'b',     '{"channel":"shop","items":[3]}'),
  (3,  'Bob  Okafor',   'south', DATE '2026-01-05', 300.00, 'widget', 'a|c',   '{"channel":"web","items":[4,5,6]}'),
  (4,  'Carla Díaz',    'east',  DATE '2026-01-09',  NULL,  'gizmo',  NULL,    '{"channel":"web","items":[]}'),
  (5,  'Bob Okafor',    'south', DATE '2026-01-12',  45.25, 'gadget', 'c',     '{"channel":"phone","items":[7]}'),
  (6,  'Dev Patel',     'west',  DATE '2026-01-12', 510.00, 'widget', 'a',     '{"channel":"web","items":[8,9]}'),
  (7,  'Carla Diaz',    'east',  DATE '2026-01-20',  99.99, 'gizmo',  'b|c',   '{"channel":"shop","items":[10]}'),
  (8,  'Dev Patel',     'west',  DATE '2026-02-02', 510.00, 'widget', 'a',     '{"channel":"web","items":[8,9]}'),
  (9,  'Eve Larsen',    NULL,    DATE '2026-02-03',  15.00, 'gadget', NULL,    '{"channel":"web","items":[11]}'),
  (10, 'Alice Ng',      'north', DATE '2026-02-10', 220.00, 'gizmo',  'a|b|c', '{"channel":"phone","items":[12,13]}')
) t(order_id, customer, region, order_date, amount, product, tags, meta);

CREATE TABLE customers AS SELECT * FROM (VALUES
  ('Alice Ng',    'retail',    DATE '2025-06-01', 52.52, 13.40),
  ('Bob Okafor',  'wholesale', DATE '2025-09-15', 48.85,  2.35),
  ('Carla Diaz',  'retail',    DATE '2025-11-30', 40.42, -3.70),
  ('Frank Moore', 'retail',    DATE '2026-01-01', 51.51, -0.13)
) t(customer, segment, joined, lat, lon);
```

Then run the statement:

```sql
INSTALL sqlite; LOAD sqlite;
INSTALL ducklake; LOAD ducklake;
ATTACH '<your_scratch_dir>/crm.sqlite' AS crm (TYPE sqlite);
CREATE TABLE crm.customers AS SELECT customer, segment FROM customers;
DETACH crm;
ATTACH '<your_scratch_dir>/warehouse.duckdb' AS warehouse;
CREATE TABLE warehouse.orders AS SELECT order_id, customer, amount FROM orders;
DETACH warehouse;
-- a DuckLake catalog: a metadata file and a folder of data files, attached by one statement
ATTACH 'ducklake:<your_scratch_dir>/lake_meta.ducklake' AS lake (DATA_PATH '<your_scratch_dir>/lake_data/');
CREATE TABLE lake.events AS SELECT order_id, 'shipped' AS status FROM orders WHERE order_id <= 4;
DETACH lake;
-- registered once, the tables are named directly, with no scan function
ATTACH '<your_scratch_dir>/crm.sqlite' AS crm (TYPE sqlite, READ_ONLY);
ATTACH '<your_scratch_dir>/warehouse.duckdb' AS warehouse (READ_ONLY);
ATTACH 'ducklake:<your_scratch_dir>/lake_meta.ducklake' AS lake (DATA_PATH '<your_scratch_dir>/lake_data/', READ_ONLY);
CREATE TEMP TABLE result AS
SELECT o.order_id, o.customer, c.segment, e.status FROM warehouse.orders o JOIN crm.customers c USING (customer) LEFT JOIN lake.events e USING (order_id);
SELECT * FROM result ORDER BY order_id;
SELECT CASE WHEN ((SELECT list(database_name ORDER BY database_name) FROM duckdb_databases() WHERE database_name IN ('crm', 'warehouse', 'lake')) = ['crm', 'lake', 'warehouse'] AND (SELECT count(*) = 4 AND count(*) FILTER (WHERE segment = 'retail') = 3 AND count(status) = 1 AND max(order_id) FILTER (WHERE status = 'shipped') = 1 FROM result)) THEN 'as expected' ELSE error('the three attached catalogs were not all registered or their tables did not join into 4 orders with a segment and one shipped') END AS verdict;
```

Replace `<your_scratch_dir>` with a directory of your own that you can write to before running the statement.

The statement writes `crm.sqlite`, `lake_data`, `lake_meta.ducklake` and `warehouse.duckdb` itself, in `<your_scratch_dir>`.

It needs the `sqlite_scanner` and `ducklake` extensions.

## What other tools call it

Meridian's classification; an independent blind rating agreed on 95.7% of 324 rows (variant, version 3).

- Apache DataFusion: [`CREATE EXTERNAL CATALOG`](https://datafusion.apache.org/user-guide/sql/ddl.html) (read 2026-09-27)
- DuckDB: [`ATTACH and DETACH`](https://duckdb.org/docs/current/sql/statements/attach) (read 2026-09-27)
- Duckle: [`DuckDB`](https://github.com/slothflowlabs/duckle/blob/f85f8960edfb6efbf4cb89d58a7b32e16df0e867/website/docs/components.html) (read 2026-09-25)
- Duckle: [`DuckLake`](https://github.com/slothflowlabs/duckle/blob/f85f8960edfb6efbf4cb89d58a7b32e16df0e867/website/docs/components.html) (read 2026-09-25)
- Ibis: [`DatabaseTable`](https://github.com/ibis-project/ibis/blob/05d2b293344e86cb41c1a426071734f7e270335a/ibis/expr/operations/relations.py) (read 2026-09-27)
- Ibis: [`UnboundTable`](https://github.com/ibis-project/ibis/blob/05d2b293344e86cb41c1a426071734f7e270335a/ibis/expr/operations/relations.py) (read 2026-09-27)
