σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
DuckDB skill›Module 2 · Working with Python›Topic 2.2·~40 minIntermediate

Persisted databases and extensions

Skill positionModule 2 of 3 · Topic 2 of 2
2.2 Persisted databases and extensions
—Lab score— / 100Total XP—Streak—
Lecture0% read

3 min readCode examples: 4

  1. 01The idea in plain words
  2. 02One file, tables and views
  3. 03Attach a file without owning it
  4. 04Export as open files
  5. 05Extensions and the catalog
  6. 06What the tasks ask

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.

01

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.

DefinitionCatalog

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().

02

One file, tables and views

main.pyPython
# 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.

03

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.

main.pyPython
# 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',)]
04

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.

main.pyPython
# 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);
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.

main.pyPython
# 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    │
└────────────────┴─────────┘
One writer, and close what you open

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.

06

What the tasks ask

  1. 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. 2
    `copy_database`EXPORT DATABASE to Parquet, then IMPORT DATABASE on the target; return the exported file names.
  3. 3
    `catalog_summary`Query duckdb_tables() and duckdb_views() for the current database only, and count each table's rows.

Key takeaways

  • A database is a file Tables and views persist when you connect to a path and close the connection.
  • Attach read-only, then detach One writer at a time; always release a file you borrowed.
  • Export is the portable form EXPORT DATABASE writes open files any tool can read. Next: query plans.
Quick check
Your notebook connected to research.duckdb, created a table and then the notebook was closed without con.close(). What is the safest assumption about the table?
Pick an answer.

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
Explorer · lab-6-1
▾ lab-6-1
▾ data
…
bash · in-browser
learner@quantcraft:~/lab-6-1$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

Everything in lab-6-1 goes back to the starter files and the terminal is cleared. Your current solution will be lost — this can't be undone.

Will be reset
  • main.py → starter code
  • Terminal history and test output
  • Unsubmitted changes
Will be kept
  • Best lab score
  • Your XP and streak
  • Lecture progress and notes