An in-memory DuckDB is gone when the notebook closes. Your research needs the cleaned prices, the derived returns and the views you built to be there tomorrow, for you and for a colleague. A DuckDB database is one file, and a database can also be exported to open Parquet files that any tool can read.
The idea in plain words
Where we are. Topic 5 moved data between libraries inside one process. This topic makes it outlive the process. The next topic looks inside a query: the plan DuckDB chooses and how to read it.
The database's own record of what it contains: its tables, views, columns, settings and attached files. DuckDB exposes it as ordinary queryable tables such as duckdb_tables(), duckdb_views() and duckdb_databases().
One file, tables and views
# Write KO to a database file, close it, and look inside from a new connectionframe = load_frame("KO")print(save_database("ko.duckdb", frame), "rows saved,", os.path.getsize("ko.duckdb") // 1024, "KB on disk")reopened = duckdb.connect("ko.duckdb", read_only=True)print(catalog_summary(reopened))print(reopened.sql("SELECT count(*) AS wide_days, round(max(spread), 3) AS widest FROM wide_days"))
6699 rows saved, 780 KB on disk
{'tables': {'bars': 6699}, 'views': ['wide_days']}
┌───────────┬────────┐
│ wide_days │ widest │
│ int64 │ double │
├───────────┼────────┤
│ 102 │ 0.148 │
└───────────┴────────┘Connecting to a file creates it if it is not there. DuckDB writes to a write-ahead log and merges it into the file on a checkpoint, which happens when you close the connection, so closing is part of saving. A view stored in the file keeps its query, so wide_days always reflects the current bars.
Attach a file without owning it
ATTACH 'file' AS src (READ_ONLY) makes another database file visible as src on a connection you already have, so one query can read it and your own tables together. Read-only is the safe default for a shared file, and DETACH releases it again: only one process can write to a file at a time, so a forgotten attachment locks out a colleague.
# The attached file is read next to an in-memory database, then releasedcon = duckdb.connect()print(query_attached(con, "ko.duckdb", "SELECT count(*), max(date) FROM src.bars"))print(con.sql("SELECT database_name FROM duckdb_databases() ORDER BY 1").fetchall())
[(6699, datetime.date(2026, 8, 21))]
[('memory',), ('system',), ('temp',)]Export as open files
EXPORT DATABASE 'folder' (FORMAT parquet) writes one Parquet file per table plus schema.sql, the statements that recreate tables and views, and load.sql, which loads the data. IMPORT DATABASE 'folder' on another connection rebuilds it. Because the files are open formats, another tool can read the same tables, and a version of DuckDB that cannot open your database file can still import the export.
# Round trip: export the KO database, import it into a fresh connectionfresh = duckdb.connect()print(copy_database(reopened, "ko_export", fresh))print(catalog_summary(fresh))print(open("ko_export/schema.sql").read().strip()[:260])
['bars.parquet', 'load.sql', 'schema.sql']
{'tables': {'bars': 6699}, 'views': ['wide_days']}
CREATE TABLE bars(date DATE, open DOUBLE, high DOUBLE, low DOUBLE, "close" DOUBLE, volume BIGINT);
CREATE VIEW wide_days AS SELECT date, ((high - low) / "close") AS spread FROM bars WHERE (((high - low) / "close") > 0.05);Extensions and the catalog
Formats and features such as Parquet, JSON and time zones come as extensions. Some are built into the DuckDB you have; others are fetched once with INSTALL name and switched on with LOAD name, which needs the network, and a few load themselves the first time you use them. duckdb_extensions() shows which are loaded, like every catalog function.
# Which extensions this DuckDB has loadedprint(fresh.sql("SELECT extension_name, loaded FROM duckdb_extensions() WHERE loaded ORDER BY 1"))
┌────────────────┬─────────┐ │ extension_name │ loaded │ │ varchar │ boolean │ ├────────────────┼─────────┤ │ core_functions │ true │ │ icu │ true │ │ json │ true │ │ parquet │ true │ └────────────────┴─────────┘
A database file can be open for writing in one process only. If a notebook keeps it open, a second notebook, a script or a colleague gets a lock error. Open read-only when you only read, close connections when done, and treat the export folder, not the live file, as the thing you share.
What the tasks ask
- 1`save_database` and `query_attached`Create a table and a view in a file and close it; attach a file read-only, query it and always detach.
- 2`copy_database`
EXPORT DATABASEto Parquet, thenIMPORT DATABASEon the target; return the exported file names. - 3`catalog_summary`Query
duckdb_tables()andduckdb_views()for the current database only, and count each table's rows.
research.duckdb, created a table and then the notebook was closed without con.close(). What is the safest assumption about the table?Lab · what's graded
4 tasks · 100 pts- Keep it in a file:
save_database25 pts - Attach, query, detach:
query_attached25 pts - Export and import:
copy_database25 pts - Ask the catalog:
catalog_summary25 pts