KevsRobots Learning Platform
28% Percent Complete
By Kevin McAleer, 2 Minutes
DuckDB runs as a single, embedded binary. Use the CLI for interactive SQL and the Python package for notebooks and scripts.
macOS (Homebrew):
brew install duckdb
Python (any OS):
python -m pip install --upgrade pip
python -m pip install duckdb
Windows (CLI):
winget install DuckDB.clichoco install duckdbLinux (CLI):
apt install duckdb (or download binaries from duckdb.org if not available)CLI:
duckdb --version
Python:
import duckdb
print(duckdb.__version__)
Start the shell and run a simple query:
duckdb
-- in the duckdb shell
SELECT 1 AS ok;
Create a persistent database file and a table:
-- creates analytics.duckdb in the current folder
.open analytics.duckdb
CREATE TABLE events(id INTEGER, ts TIMESTAMP, type TEXT);
INSERT INTO events VALUES (1, now(), 'start');
SELECT COUNT(*) AS rows FROM events;
Exit with .quit.
You try it (2 min)
- Start the CLI, run
SELECT 42 AS answer;- Create
analytics.duckdb, thenCREATE TABLE numbers AS SELECT * FROM range(5) AS x;SELECT COUNT(*) FROM numbers;and exit with.quit
import duckdb
# Connect to an on-disk database (creates file if it doesn't exist)
con = duckdb.connect("analytics.duckdb")
con.execute("CREATE TABLE IF NOT EXISTS numbers AS SELECT * FROM range(10) AS x;")
rows = con.execute("SELECT COUNT(*) FROM numbers").fetchone()[0]
print({"rows": rows})
con.close()
You try it (2 min)
- Change
range(10)torange(7)and print the new count- Create a new table
evenswith only even values fromnumbers
.duckdb file you can version control.Troubleshooting? See Troubleshooting DuckDB for common fixes (exports folder, SSL, Python, S3).
You can use the arrows ← → on your keyboard to navigate between lessons.
Comments