### DuckDB Install Script Output Example Source: https://duckdb.org/docs/current/operations_manual/installing_duckdb/install_script This is an example of the output you might see when running the DuckDB install script on Linux or macOS. ```text % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 3507 100 3507 0 0 34367 0 --:--:-- --:--:-- --:--:-- 34382 https://install.duckdb.org/v1.4.1/duckdb_cli-osx-universal.gz *** DuckDB Linux/MacOS installation script, version 1.4.1 *** .;odxdl, .xXXXXXXXXKc 0XXXXXXXXXXXd cooo: ,XXXXXXXXXXXXK OXXXXd 0XXXXXXXXXXXo cooo: .xXXXXXXXXKc .;odxdl, ######################################################################## 100.0% Successfully installed DuckDB binary to /Users/your_user/.duckdb/cli/1.4.1/duckdb with a link from /Users/your_user/.duckdb/cli/latest/duckdb Hint: Append the following line to your shell profile: export PATH='/Users/your_user/.duckdb/cli/latest':$PATH To launch DuckDB now, type /Users/your_user/.duckdb/cli/latest/duckdb ``` -------------------------------- ### Install DuckDB Go Client Source: https://duckdb.org/docs/current/clients/go.html Use `go get` to install the latest stable version of the DuckDB Go client. ```bash go get github.com/duckdb/duckdb-go/v2 ``` -------------------------------- ### Go: In-memory DuckDB ADBC Client Example Source: https://duckdb.org/docs/current/clients/adbc.html This example shows how to create an in-memory DuckDB database, import an Arrow RecordBatch, run a SQL query against it, and process the results. Ensure the libduckdb library is installed. ```Go package main import ( "bytes" "context" "fmt" "io" "github.com/apache/arrow-adbc/go/adbc" "github.com/apache/arrow-adbc/go/adbc/drivermgr" "github.com/apache/arrow-go/v18/arrow" "github.com/apache/arrow-go/v18/arrow/array" "github.com/apache/arrow-go/v18/arrow/ipc" "github.com/apache/arrow-go/v18/arrow/memory" ) func _makeSampleArrowRecord() arrow.Record { b := array.NewFloat64Builder(memory.DefaultAllocator) b.AppendValues([]float64{1, 2, 3}, nil) col := b.NewArray() defer col.Release() defer b.Release() schema := arrow.NewSchema([]arrow.Field{{Name: "column1", Type: arrow.PrimitiveTypes.Float64}}, nil) return array.NewRecord(schema, []arrow.Array{col}, int64(col.Len())) } type DuckDBSQLRunner struct { ctx context.Context conn adbc.Connection db adbc.Database } func NewDuckDBSQLRunner(ctx context.Context) (*DuckDBSQLRunner, error) { var drv drivermgr.Driver db, err := drv.NewDatabase(map[string]string{ "driver": "duckdb", "entrypoint": "duckdb_adbc_init", "path": ":memory:", }) if err != nil { return nil, fmt.Errorf("failed to create new in-memory DuckDB database: %w", err) } conn, err := db.Open(ctx) if err != nil { return nil, fmt.Errorf("failed to open connection to new in-memory DuckDB database: %w", err) } return &DuckDBSQLRunner{ctx: ctx, conn: conn, db: db}, nil } func serializeRecord(record arrow.Record) (io.Reader, error) { buf := new(bytes.Buffer) wr := ipc.NewWriter(buf, ipc.WithSchema(record.Schema())) if err := wr.Write(record); err != nil { return nil, fmt.Errorf("failed to write record: %w", err) } if err := wr.Close(); err != nil { return nil, fmt.Errorf("failed to close writer: %w", err) } return buf, nil } func (r *DuckDBSQLRunner) importRecord(sr io.Reader) error { rdr, err := ipc.NewReader(sr) if err != nil { return fmt.Errorf("failed to create IPC reader: %w", err) } defer rdr.Release() _, err = adbc.IngestStream(r.ctx, r.conn, rdr, "temp_table", adbc.OptionValueIngestModeCreate, adbc.IngestStreamOptions{}) return err } func (r *DuckDBSQLRunner) runSQL(sql string) ([]arrow.Record, error) { stmt, err := r.conn.NewStatement() if err != nil { return nil, fmt.Errorf("failed to create new statement: %w", err) } defer stmt.Close() if err := stmt.SetSqlQuery(sql); err != nil { return nil, fmt.Errorf("failed to set SQL query: %w", err) } out, n, err := stmt.ExecuteQuery(r.ctx) if err != nil { return nil, fmt.Errorf("failed to execute query: %w", err) } defer out.Release() result := make([]arrow.Record, 0, n) for out.Next() { rec := out.Record() rec.Retain() // .Next() will release the record, so we need to retain it result = append(result, rec) } if out.Err() != nil { return nil, out.Err() } return result, nil } func (r *DuckDBSQLRunner) RunSQLOnRecord(record arrow.Record, sql string) ([]arrow.Record, error) { serializedRecord, err := serializeRecord(record) if err != nil { return nil, fmt.Errorf("failed to serialize record: %w", err) } if err := r.importRecord(serializedRecord); err != nil { return nil, fmt.Errorf("failed to import record: %w", err) } result, err := r.runSQL(sql) if err != nil { return nil, fmt.Errorf("failed to run SQL: %w", err) } if _, err := r.runSQL("DROP TABLE temp_table"); err != nil { return nil, fmt.Errorf("failed to drop temp table after running query: %w", err) } return result, nil } func (r *DuckDBSQLRunner) Close() { r.conn.Close() r.db.Close() } func main() { rec := _makeSampleArrowRecord() fmt.Println(rec) runner, err := NewDuckDBSQLRunner(context.Background()) if err != nil { panic(err) } defer runner.Close() resultRecords, err := runner.RunSQLOnRecord(rec, "SELECT column1+1 FROM temp_table") if err != nil { panic(err) } for _, resultRecord := range resultRecords { fmt.Println(resultRecord) resultRecord.Release() } } ``` -------------------------------- ### Example: Compute Quadkey Source: https://duckdb.org/docs/current/core_extensions/spatial/functions.html Example demonstrating how to compute a quadkey for a specific point and level. ```sql SELECT ST_QuadKey(st_point(11.08, 49.45), 10); ---- ``` ```sql 1333203202 ``` -------------------------------- ### Create LineString Example Source: https://duckdb.org/docs/current/core_extensions/spatial/functions.html Example showing how to create a LINESTRING from a list of points. ```sql SELECT ST_MakeLine([ST_Point(0, 0), ST_Point(1, 1)]); ---- ``` ```sql LINESTRING(0 0, 1 1) ``` -------------------------------- ### Create Polygon Example Source: https://duckdb.org/docs/current/core_extensions/spatial/functions.html Example showing the creation of a POLYGON from a LINESTRING representing its shell. ```sql SELECT ST_MakePolygon(ST_LineString([ST_Point(0, 0), ST_Point(1, 0), ST_Point(1, 1), ST_Point(0, 0)])); ``` -------------------------------- ### Create Point Example Source: https://duckdb.org/docs/current/core_extensions/spatial/functions.html Example demonstrating the creation of a POINT geometry using longitude and latitude. ```sql SELECT ST_AsText(ST_MakePoint(143.3, -24.2)); ---- ``` ```sql POINT (143.3 -24.2) ``` -------------------------------- ### Create 2D Box Example Source: https://duckdb.org/docs/current/core_extensions/spatial/functions.html Example demonstrating the creation of a BOX2D from two points. ```sql SELECT ST_MakeBox2D(ST_Point(0, 0), ST_Point(1, 1)); ---- ``` ```sql BOX(0 0, 1 1) ``` -------------------------------- ### Install Quack Extension Source: https://duckdb.org/docs/current/core_extensions/quack.html Use this command to explicitly install the Quack extension if automatic installation is not desired. ```sql INSTALL quack; ``` -------------------------------- ### Install and Load MotherDuck Extension Source: https://duckdb.org/docs/current/core_extensions/motherduck.html Manually install and load the MotherDuck extension using its name or shorthand. ```sql INSTALL md; LOAD md; ``` -------------------------------- ### install_extension Source: https://duckdb.org/docs/current/clients/python/reference Installs an extension by name, with options for specifying version, repository, and forcing installation. ```APIDOC ## install_extension ### Description Install an extension by name, with an optional version and/or repository to get the extension from. ### Parameters - **_extension** (str) - Required - The name of the extension to install. - **_force_install** (bool) - Optional - Defaults to False. If True, forces the installation even if the extension is already installed. - **_repository** (object) - Optional - The repository object to get the extension from. - **_repository_url** (object) - Optional - The URL of the repository to get the extension from. - **_version** (object) - Optional - The version of the extension to install. ``` -------------------------------- ### Install Development Dependencies with uv Source: https://duckdb.org/docs/current/dev/building/python.html Installs all development dependencies for the project without building it. This is the first step in setting up the environment. ```bash uv sync --no-install-project ``` -------------------------------- ### DuckDB C API: Prepared Statement Example Source: https://duckdb.org/docs/current/clients/c/prepared Demonstrates how to prepare, bind parameters, execute, and clean up prepared statements for both INSERT and SELECT operations in DuckDB using the C API. Parameter indices start at 1. NULL for the result set pointer indicates no result set is expected. ```c duckdb_prepared_statement stmt; duckdb_result result; if (duckdb_prepare(con, "INSERT INTO integers VALUES ($1, $2)", &stmt) == DuckDBError) { // handle error } duckdb_bind_int32(stmt, 1, 42); // the parameter index starts counting at 1! duckdb_bind_int32(stmt, 2, 43); // NULL as second parameter means no result set is requested duckdb_execute_prepared(stmt, NULL); duckdb_destroy_prepare(&stmt); // we can also query result sets using prepared statements if (duckdb_prepare(con, "SELECT * FROM integers WHERE i = ?", &stmt) == DuckDBError) { // handle error } duckdb_bind_int32(stmt, 1, 42); duckdb_execute_prepared(stmt, &result); // do something with result // clean up duckdb_destroy_result(&result); duckdb_destroy_prepare(&stmt); ``` -------------------------------- ### Start Quack Server Source: https://duckdb.org/docs/current/quack/setup/reverse_proxy.html This command starts the Quack server, which will print an authentication token. It binds to the specified address and port. ```sql CALL quack_serve('quack:localhost'); ``` -------------------------------- ### Install and Load httpfs Extension Source: https://duckdb.org/docs/current/core_extensions/httpfs/overview.html Manually install and load the httpfs extension in DuckDB. ```sql INSTALL httpfs; LOAD httpfs; ``` -------------------------------- ### Install and Load TPC-H Extension Source: https://duckdb.org/docs/current/core_extensions/tpch.html Installs and loads the TPC-H extension. This is necessary if the extension is not shipped by default or not autoloaded. ```sql INSTALL tpch; LOAD tpch; ``` -------------------------------- ### Install and Load Unity Catalog Extension Source: https://duckdb.org/docs/current/core_extensions/unity_catalog.html Use these commands to install and load the unity_catalog extension in DuckDB. Ensure you have the necessary permissions. ```sql INSTALL unity_catalog; LOAD unity_catalog; ``` -------------------------------- ### Install and Run Caddy Source: https://duckdb.org/docs/current/quack/setup/reverse_proxy.html Instructions for installing Caddy on macOS using Homebrew and running it with a specified configuration file. The first run may require elevated privileges to install Caddy's local Certificate Authority. ```bash brew install caddy # macOS, for other platforms see https://caddyserver.com/docs/install caddy run --config Caddyfile ``` -------------------------------- ### Windows Dynamic Prompt Example Source: https://duckdb.org/docs/current/clients/cli/friendly_cli.html Example of the dynamic prompt format used on Windows systems. ```text -- Windows {max_length:40}{color:green}{color:bold}{setting:current_database_and_schema}{color:reset} D ``` -------------------------------- ### macOS / Linux Dynamic Prompt Example Source: https://duckdb.org/docs/current/clients/cli/friendly_cli.html Example of the dynamic prompt format used on macOS and Linux systems. ```text -- macOS / Linux {max_length:40}{color:38,5,208}{color:bold}{setting:current_database_and_schema}{color:reset} D ``` -------------------------------- ### Install DuckDB PHP Client Source: https://duckdb.org/docs/current/clients/tertiary_clients/php.html Use Composer to install the DuckDB PHP client. This is the recommended method for newcomers. ```bash composer require satur.io/duckdb-auto ``` -------------------------------- ### Install and Load Delta Extension Source: https://duckdb.org/docs/current/core_extensions/delta.html Manually install and load the delta extension if automatic autoloading is not desired. ```sql INSTALL delta; LOAD delta; ``` -------------------------------- ### Install DuckDB CLI on Windows Source: https://duckdb.org/docs/current/operations_manual/installing_duckdb/install_script Run this PowerShell command to install the beta version of the DuckDB CLI client on Windows. ```powershell powershell -NoExit iex (iwr "https://install.duckdb.org/install.ps1").Content ``` -------------------------------- ### Install Required Extensions Source: https://duckdb.org/docs/current/core_extensions/iceberg/amazon_s3_tables.html Install the necessary extensions for AWS, HTTPFS, and Iceberg support in DuckDB. ```sql INSTALL aws; INSTALL httpfs; INSTALL iceberg; ``` -------------------------------- ### Example of 8-Bit Colors Output Source: https://duckdb.org/docs/current/clients/cli/friendly_cli.html An example of the output format when displaying 8-bit colors in the CLI. ```text darkred1 red darkred2 red3 red4 red1 brightred indianred1 ... ``` -------------------------------- ### Install and Load AWS Extension Source: https://duckdb.org/docs/current/core_extensions/aws.html Installs and loads the AWS extension manually. This is typically autoloaded but can be managed explicitly. ```sql INSTALL aws; LOAD aws; ``` -------------------------------- ### Install and Load vss Extension, Create Table, and Index Source: https://duckdb.org/docs/current/core_extensions/vss.html Installs and loads the vss extension, creates a sample table with a vector column, inserts data, and creates an HNSW index on the vector column. ```sql INSTALL vss; LOAD vss; CREATE TABLE my_vector_table (vec FLOAT[3]); INSERT INTO my_vector_table SELECT array_value(a, b, c) FROM range(1, 10) ra(a), range(1, 10) rb(b), range(1, 10) rc(c); CREATE INDEX my_hnsw_index ON my_vector_table USING HNSW (vec); ``` -------------------------------- ### Install unixODBC on Debian/Ubuntu Source: https://duckdb.org/docs/current/core_extensions/odbc/overview.html Installs the unixODBC Driver Manager on Debian-based Linux distributions using apt-get. ```bash sudo apt-get install unixodbc ``` -------------------------------- ### Install and Load Community Extension Source: https://duckdb.org/docs/current/clients/python/overview.html Installs and loads a community extension (e.g., 'h3') using the 'community' repository. ```python import duckdb con = duckdb.connect() con.install_extension("h3", repository="community") con.load_extension("h3") ``` -------------------------------- ### Build and Install Project with uv Source: https://duckdb.org/docs/current/dev/building/python.html Builds and installs the project after dependencies are set up, without using build isolation. This ensures proper CMake integration. ```bash uv sync --no-build-isolation ``` -------------------------------- ### unixodbc_setup.sh Usage Example Source: https://duckdb.org/docs/current/clients/odbc/linux.html Demonstrates how to use the unixodbc_setup.sh script with specific database and driver paths. ```bash Usage: ./unixodbc_setup.sh [options] Example: ./unixodbc_setup.sh -u -db ~/database_path -D ~/driver_path/libduckdb_odbc.so Level: -s: System-level, using 'sudo' to configure DuckDB ODBC at the system-level, changing the files: /etc/odbc[inst].ini -u: User-level, configuring the DuckDB ODBC at the user-level, changing the files: ~/.odbc[inst].ini. Options: -db database_path>: the DuckDB database file path, the default is ':memory:' if not provided. -D driver_path: the driver file path (i.e., the path for libduckdb_odbc.so), the default is using the base script directory ``` -------------------------------- ### Install Alpine Linux CLI Client Prerequisites Source: https://duckdb.org/docs/current/dev/building/linux.html Installs necessary packages for building the DuckDB CLI client on Alpine Linux. ```bash apk add g++ git make cmake ninja git clone https://github.com/duckdb/duckdb cd duckdb GEN=ninja make ``` -------------------------------- ### Display unixodbc_setup.sh Help Source: https://duckdb.org/docs/current/clients/odbc/linux.html Shows the usage instructions and available options for the unixodbc_setup.sh script. ```bash ./unixodbc_setup.sh --help ``` -------------------------------- ### Install and Load FTS Extension Source: https://duckdb.org/docs/current/core_extensions/full_text_search.html Installs and loads the FTS extension. It is usually autoloaded on first use from the official repository. ```sql INSTALL fts; LOAD fts; ``` -------------------------------- ### Install Older Red Hat-based CLI Client Prerequisites Source: https://duckdb.org/docs/current/dev/building/linux.html Installs packages and configures build for older Red Hat-based systems, potentially requiring manual Make job configuration. ```bash sudo yum install -y git gcc-c++ cmake openssl-devel git clone https://github.com/duckdb/duckdb cd duckdb mkdir build cd build cmake .. make -j`nproc` ``` -------------------------------- ### Install and Load INET Extension Source: https://duckdb.org/docs/current/core_extensions/inet.html Manually install and load the INET extension if it's not autoloaded. This is useful for explicit control over extension management. ```sql INSTALL inet; LOAD inet; ``` -------------------------------- ### Install Ubuntu/Debian CLI Client Prerequisites Source: https://duckdb.org/docs/current/dev/building/linux.html Installs necessary packages for building the DuckDB CLI client on Ubuntu, Debian, and similar distributions. ```bash sudo apt-get update sudo apt-get install -y git g++ cmake ninja-build libssl-dev libcurl4-openssl-dev git clone https://github.com/duckdb/duckdb cd duckdb GEN=ninja make ``` -------------------------------- ### Simple DuckDB Operations in Go Source: https://duckdb.org/docs/current/clients/go.html A basic example showing how to open a connection, create a table, insert data, and query it using the `database/sql` interface. ```go package main import ( "database/sql" "errors" "fmt" "log" _ "github.com/duckdb/duckdb-go/v2" ) func main() { db, err := sql.Open("duckdb", "") if err != nil { log.Fatal(err) } defer db.Close() _, err = db.Exec(`CREATE TABLE people (id INTEGER, name VARCHAR)`) if err != nil { log.Fatal(err) } _, err = db.Exec(`INSERT INTO people VALUES (42, 'John')`) if err != nil { log.Fatal(err) } var ( id int name string ) row := db.QueryRow(`SELECT id, name FROM people`) err = row.Scan(&id, &name) if errors.Is(err, sql.ErrNoRows) { log.Println("no rows") } else if err != nil { log.Fatal(err) } fmt.Printf("id: %d, name: %s\n", id, name) } ``` -------------------------------- ### DuckDB Python bit_or Aggregation Example Source: https://duckdb.org/docs/current/clients/python/relational_api.html Computes the bitwise OR of all bits in a column, grouped by another column. Requires DuckDB to be installed and connected. ```python import duckdb duckdb_conn = duckdb.connect() rel = duckdb_conn.sql(""" select gen_random_uuid() as id, concat('value is ', case when mod(range,2)=0 then 'even' else 'uneven' end) as description, range as value, now() + concat(range,' ', 'minutes')::interval as created_timestamp from range(1, 10) """) rel = rel.select("description, value::bit as value_bit") rel.bit_or(column="value_bit", groups="description", projected_columns="description") ``` -------------------------------- ### SQL Autocomplete Example Source: https://duckdb.org/docs/current/core_extensions/autocomplete.html Use the `sql_auto_complete` function to get suggestions for a given query string. This is useful for building interactive SQL interfaces. ```sql SELECT * FROM sql_auto_complete('SEL'); ``` -------------------------------- ### DuckDB Python bit_xor Aggregation Example Source: https://duckdb.org/docs/current/clients/python/relational_api.html Computes the bitwise XOR of all bits in a column, grouped by another column. Requires DuckDB to be installed and connected. ```python import duckdb duckdb_conn = duckdb.connect() rel = duckdb_conn.sql(""" select gen_random_uuid() as id, concat('value is ', case when mod(range,2)=0 then 'even' else 'uneven' end) as description, range as value, now() + concat(range,' ', 'minutes')::interval as created_timestamp from range(1, 10) """) rel = rel.select("description, value::bit as value_bit") rel.bit_xor(column="value_bit", groups="description", projected_columns="description") ``` -------------------------------- ### Example Build Output Source: https://duckdb.org/docs/current/dev/building/build_configuration.html Shows the expected output when running the build with an overridden Git hash and version. ```text v0.10.1-dev843 09ea97d0a9 ... ``` -------------------------------- ### Control Task Evaluation with Pending Results Source: https://duckdb.org/docs/current/clients/node_neo/overview.html Manage the asynchronous execution of queries using `prepare` and `start` to get a pending result. Poll `runTask` until `RESULT_READY` and then retrieve the final result with `getResult`. ```javascript import { DuckDBPendingResultState } from '@duckdb/node-api'; async function sleep(ms) { return new Promise((resolve) => { setTimeout(resolve, ms); }); } const prepared = await connection.prepare('from range(10_000_000)'); const pending = prepared.start(); while (pending.runTask() !== DuckDBPendingResultState.RESULT_READY) { console.log('not ready'); await sleep(1); } console.log('ready'); const result = await pending.getResult(); // ... ``` -------------------------------- ### Get Result Data in Various Formats Source: https://duckdb.org/docs/current/clients/node_neo/overview.html Retrieve query results as arrays of rows, arrays of row objects, arrays of columns, or column objects. This example demonstrates fetching data from a simple range query. ```javascript const reader = await connection.runAndReadAll( 'from range(3) select range::int as i, 10 + i as n' ); const rows = reader.getRows(); // [ [0, 10], [1, 11], [2, 12] ] const rowObjects = reader.getRowObjects(); // [ { i: 0, n: 10 }, { i: 1, n: 11 }, { i: 2, n: 12 } ] const columns = reader.getColumns(); // [ [0, 1, 2], [10, 11, 12] ] const columnsObject = reader.getColumnsObject(); // { i: [0, 1, 2], n: [10, 11, 12] } ``` -------------------------------- ### Configure File Logging: Normalized vs. Denormalized Source: https://duckdb.org/docs/current/operations_manual/logging/overview.html Demonstrates how to configure file logging for normalized and denormalized formats. Normalized logging creates separate context and entry files, while denormalized logs to a single file. ```sql -- normalized: creates `/tmp/duckdb_log_contexts.csv` and `/tmp/duckdb_log_entries.csv` CALL enable_logging(storage_path = '/tmp'); -- denormalized: creates `/tmp/logs.csv` CALL enable_logging(storage_path = '/tmp/logs.csv'); ``` -------------------------------- ### Initialize Database Source: https://duckdb.org/docs/current/clients/adbc.html Finalizes the database setup by applying all set options and initializing the database connection. ```c AdbcDatabaseInit(&adbc_database, &adbc_error) ``` -------------------------------- ### Install and Load Lance Extension Source: https://duckdb.org/docs/current/core_extensions/lance.html Install the Lance extension from DuckDB's core extensions repository and load it into the current session. ```sql INSTALL lance; LOAD lance; ``` -------------------------------- ### Install DuckDB R Client Source: https://duckdb.org/docs/current/clients/r.html Installs the DuckDB R client package. Ensure you have R and the necessary build tools installed. ```r install.packages("duckdb") ``` -------------------------------- ### Setting DuckDB Configuration Options Source: https://duckdb.org/docs/current/clients/c/config Demonstrates how to create a configuration object, set various options such as access mode, threads, max memory, and default order, and then open the database with these configurations. Remember to destroy the configuration object afterwards. ```c duckdb_database db; duckdb_config config; // create the configuration object if (duckdb_create_config(&config) == DuckDBError) { // handle error } // set some configuration options duckdb_set_config(config, "access_mode", "READ_WRITE"); // or READ_ONLY duckdb_set_config(config, "threads", "8"); duckdb_set_config(config, "max_memory", "8GB"); duckdb_set_config(config, "default_order", "DESC"); // open the database using the configuration if (duckdb_open_ext(NULL, &db, config, NULL) == DuckDBError) { // handle error } // cleanup the configuration object duckdb_destroy_config(&config); // run queries... // cleanup duckdb_close(&db); ``` -------------------------------- ### Install DuckDB Extension Source: https://duckdb.org/docs/current/clients/python/reference Install a DuckDB extension by its name. Options include forcing installation, specifying a repository, or setting a specific version. ```python duckdb.install_extension(_extension : str_, _*_ , _force_install : bool = False_, _repository : object = None_, _repository_url : object = None_, _version : object = None_, _connection : duckdb.DuckDBPyConnection = None_) → None¶ ``` -------------------------------- ### Read CSV File Example Source: https://duckdb.org/docs/current/operations_manual/securing_duckdb/overview.html Demonstrates reading a CSV file, which can be restricted using DuckDB settings. This example shows accessing the '/etc/passwd' file. ```sql SELECT * FROM read_csv('/etc/passwd', sep = ':'); ``` -------------------------------- ### Initialize ADBC Connection Source: https://duckdb.org/docs/current/clients/adbc.html Use `ConnectionInit` to finalize connection options and establish the connection to the database. ```c AdbcConnectionInit(&adbc_connection, &adbc_database, &adbc_error) ``` -------------------------------- ### Install x86_64 unixODBC on macOS with Rosetta Source: https://duckdb.org/docs/current/core_extensions/odbc/overview.html Installs the x86_64 version of unixODBC on macOS using a specific Homebrew installation for Rosetta compatibility. ```bash arch -x86_64 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" /usr/local/bin/brew install unixodbc ``` -------------------------------- ### Install Arch Linux CLI Client Prerequisites Source: https://duckdb.org/docs/current/dev/building/linux.html Installs necessary packages for building the DuckDB CLI client on Arch Linux and similar distributions. ```bash sudo pacman -S git gcc cmake ninja openssl git clone https://github.com/duckdb/duckdb cd duckdb GEN=ninja make ``` -------------------------------- ### Install DuckDB from R-universe Source: https://duckdb.org/docs/current/clients/r.html If you encounter installation warnings on macOS, consider installing DuckDB from the R-universe repository. This can help resolve dependency issues. ```r install.packages("duckdb", repos = c("https://duckdb.r-universe.dev", "https://cloud.r-project.org")) ``` -------------------------------- ### Install Termux Packages for Building Source: https://duckdb.org/docs/current/dev/building/android.html Install necessary build tools like git, ninja, clang, cmake, and python3 within the Termux environment. ```bash pkg install -y git ninja clang cmake python3 ``` -------------------------------- ### Install Latest Iceberg Extension for DuckDB Source: https://duckdb.org/docs/current/core_extensions/iceberg/troubleshooting Ensure you have the latest Iceberg extension installed to resolve curl request failures. Exit DuckDB and restart a new session after installation. ```duckdb FORCE INSTALL iceberg FROM core_nightly; ``` ```duckdb LOAD iceberg; ``` -------------------------------- ### Verify DuckDB Python Installation Source: https://duckdb.org/docs/current/dev/building/python.html Runs a simple SQL query using the installed DuckDB Python package to verify the installation. This checks if the package can be imported and basic SQL execution works. ```python uv run python -c "import duckdb; print(duckdb.sql('SELECT 42').fetchall())" ``` -------------------------------- ### Initialize CLI with a Custom File Source: https://duckdb.org/docs/current/clients/cli/overview.html Use the `-init` flag with the `duckdb` command to specify an initialization file (e.g., `prompt.sql`) that contains commands to be executed on startup. ```bash duckdb -init prompt.sql ``` -------------------------------- ### Initialize and Connect to DuckDB Database Source: https://duckdb.org/docs/current/clients/c/connect.html Demonstrates the basic steps to open a DuckDB database (in-memory or file-based) and establish a connection. Ensure to disconnect and close resources to prevent leaks. ```c duckdb_database db; duckdb_connection con; if (duckdb_open(NULL, &db) == DuckDBError) { // handle error } if (duckdb_connect(db, &con) == DuckDBError) { // handle error } // run queries... // cleanup duckdb_disconnect(&con); duckdb_close(&db); ``` -------------------------------- ### Install ODBC Extension from Nightly Build URL Source: https://duckdb.org/docs/current/core_extensions/odbc/overview.html Installs the ODBC extension from a provided URL, typically used for nightly builds or specific versions. The URL includes version and platform information. ```sql INSTALL 'http://nightly-extensions.duckdb.org/v1.2.0/platform/odbc_scanner.duckdb_extension.gz'; ``` -------------------------------- ### Create, Insert, and Fetch Data in DuckDB Source: https://duckdb.org/docs/current/clients/python/dbapi.html Demonstrates creating a table, inserting data, and retrieving all results using fetchall. Also shows retrieving data row by row using fetchone until the transaction closes. ```python # create a table con.execute("CREATE TABLE items (item VARCHAR, value DECIMAL(10, 2), count INTEGER)") # insert two items into the table con.execute("INSERT INTO items VALUES ('jeans', 20.0, 1), ('hammer', 42.2, 2)") # retrieve the items again con.execute("SELECT * FROM items") print(con.fetchall()) # [('jeans', Decimal('20.00'), 1), ('hammer', Decimal('42.20'), 2)] # retrieve the items one at a time con.execute("SELECT * FROM items") print(con.fetchone()) # ('jeans', Decimal('20.00'), 1) print(con.fetchone()) # ('hammer', Decimal('42.20'), 2) print(con.fetchone()) # This closes the transaction. Any subsequent calls to .fetchone will return None # None ``` -------------------------------- ### C Appender Example Source: https://duckdb.org/docs/current/clients/c/appender.html Demonstrates creating a table, initializing an appender, appending two rows with integer and varchar data, and then destroying the appender. ```c duckdb_query(con, "CREATE TABLE people (id INTEGER, name VARCHAR)", NULL); duckdb_appender appender; if (duckdb_appender_create(con, NULL, "people", &appender) == DuckDBError) { // handle error } // append the first row (1, Mark) duckdb_append_int32(appender, 1); duckdb_append_varchar(appender, "Mark"); duckdb_appender_end_row(appender); // append the second row (2, Hannes) duckdb_append_int32(appender, 2); duckdb_append_varchar(appender, "Hannes"); duckdb_appender_end_row(appender); // finish appending and flush all the rows to the table duckdb_appender_destroy(&appender); ``` -------------------------------- ### install_extension Source: https://duckdb.org/docs/current/clients/python/reference Installs a DuckDB extension by its name. Supports specifying version, repository, and forcing installation. ```APIDOC ## install_extension ### Description Installs an extension by name. This function allows for specifying an optional version and/or repository to fetch the extension from, and can force the installation. ### Method `duckdb.install_extension` ### Parameters - **extension** (str) - Required - The name of the extension to install. - **force_install** (bool) - Optional - If True, forces the installation even if the extension is already installed. Defaults to False. - **repository** (object) - Optional - Specifies the repository to install the extension from. - **repository_url** (object) - Optional - The URL of the repository. - **version** (object) - Optional - The specific version of the extension to install. - **connection** (duckdb.DuckDBPyConnection) - Optional - The DuckDB connection to use for this operation. ``` -------------------------------- ### Install Build Dependencies Source: https://duckdb.org/docs/current/dev/building/macos.html Installs Git, CMake, and Ninja, which are required for building DuckDB on macOS. ```bash brew install git cmake ninja ``` -------------------------------- ### Install MySQL Extension Source: https://duckdb.org/docs/current/core_extensions/mysql.html Installs the MySQL extension for DuckDB. This is a prerequisite for using MySQL functionalities. ```sql INSTALL mysql; ``` -------------------------------- ### Example PostgreSQL Connection String Parameters Source: https://duckdb.org/docs/current/core_extensions/postgres/overview.html Illustrates common parameters for configuring a PostgreSQL connection string. These parameters control aspects like database name, host, port, and timeouts. ```sql dbname=postgresscanner host=localhost port=5432 dbname=mydb connect_timeout=10 ``` -------------------------------- ### Execute and Connect to Default Connection Source: https://duckdb.org/docs/current/clients/python/dbapi.html Demonstrates executing a SQL command and then querying a table using the default in-memory connection. ```python import duckdb duckdb.execute("CREATE TABLE tbl AS SELECT 42 a") con = duckdb.connect(":default:") con.sql("SELECT * FROM tbl") # or duckdb.default_connection().sql("SELECT * FROM tbl") ``` -------------------------------- ### Install unixODBC on Fedora-based Systems Source: https://duckdb.org/docs/current/clients/odbc/linux.html Installs the unixODBC driver manager on Fedora-based Linux distributions. ```bash sudo yum install unixODBC ``` -------------------------------- ### Example of column_types parameter Source: https://duckdb.org/docs/current/core_extensions/odbc/functions.html Shows how to specify custom column types for table creation using the `column_types` parameter when `create_table` is enabled. ```sql create_table=TRUE, column_types=MAP { 'DUCKDB_TYPE_VARCHAR': 'VARCHAR2(10)', 'DUCKDB_TYPE_DECIMAL': 'NUMBER({typmod1},{typmod2})'} ``` -------------------------------- ### Connect to Different Database Types Source: https://duckdb.org/docs/current/clients/python/dbapi.html Shows how to establish connections to in-memory databases, persistent file-based databases (read-write and read-only), and explicitly retrieve the default connection. ```python import duckdb # to start an in-memory database con = duckdb.connect(database = ":memory:") # to use a database file (not shared between processes) con = duckdb.connect(database = "my-db.duckdb", read_only = False) # to use a database file (shared between processes) con = duckdb.connect(database = "my-db.duckdb", read_only = True) # to explicitly get the default connection con = duckdb.connect(database = ":default:") ``` -------------------------------- ### Create CSV File Source: https://duckdb.org/docs/current/clients/cli/overview.html This command creates an example CSV file named 'test.csv' with a header and a single column 'woot'. ```sql COPY (SELECT 42 AS woot UNION ALL SELECT 43 AS woot) TO 'test.csv' (HEADER); ``` -------------------------------- ### Configure DuckDB ODBC (User-level) Source: https://duckdb.org/docs/current/clients/odbc/linux.html Runs the setup script to configure the DuckDB ODBC driver for the current user. ```bash ./unixodbc_setup.sh -u ``` -------------------------------- ### Install OpenSSL for httpfs Extension Source: https://duckdb.org/docs/current/dev/building/linux.html Installs the `libssl-dev` package required for building the `httpfs` extension on Linux. ```bash sudo apt-get install -y libssl-dev ``` -------------------------------- ### Install unixODBC on Alpine Linux Source: https://duckdb.org/docs/current/core_extensions/odbc/overview.html Installs the unixODBC Driver Manager on Alpine Linux using apk. ```bash sudo apk add unixodbc ``` -------------------------------- ### Build DuckDB Go Client on Windows Source: https://duckdb.org/docs/current/dev/building/windows.html Configure environment variables and build the DuckDB Go client on Windows. Requires specific library files to be placed in a designated directory. ```bash set PATH=C:\duckdb-go\libs\;%PATH% set CGO_CFLAGS=-IC:\duckdb-go\libs\ set CGO_LDFLAGS=-LC:\duckdb-go\libs\ -lduckdb go build ``` -------------------------------- ### Configure DuckDB ODBC (System-level) Source: https://duckdb.org/docs/current/clients/odbc/linux.html Runs the setup script with root privileges to configure the DuckDB ODBC driver for all users. ```bash sudo ./unixodbc_setup.sh -s ``` -------------------------------- ### Start UI from SQL Source: https://duckdb.org/docs/current/core_extensions/ui.html Execute this SQL command within DuckDB to initiate the UI. The UI will open in your default browser. ```sql CALL start_ui(); ``` -------------------------------- ### Enable Profiling with Unnamed Metrics Source: https://duckdb.org/docs/current/dev/profiling.html This example shows how to enable profiling by passing a list of metrics as an unnamed parameter to the enable_profiling function. ```sql CALL enable_profiling(['LATENCY', 'RESULT_SET_SIZE']); ``` -------------------------------- ### Install and Load SQLite Extension Source: https://duckdb.org/docs/current/core_extensions/sqlite.html Installs and loads the SQLite extension for DuckDB. This is often autoloaded but can be managed manually. ```sql INSTALL sqlite; LOAD sqlite; ``` -------------------------------- ### Instantiate DuckDB-Wasm with Statically Served Files Source: https://duckdb.org/docs/current/clients/wasm/instantiation This example shows how to instantiate DuckDB-Wasm when you have manually downloaded and are serving the distribution files yourself. ```typescript import * as duckdb from '@duckdb/duckdb-wasm'; const MANUAL_BUNDLES: duckdb.DuckDBBundles = { mvp: { mainModule: 'change/me/../duckdb-mvp.wasm', mainWorker: 'change/me/../duckdb-browser-mvp.worker.js', }, eh: { mainModule: 'change/m/../duckdb-eh.wasm', mainWorker: 'change/m/../duckdb-browser-eh.worker.js', }, }; // Select a bundle based on browser checks const bundle = await duckdb.selectBundle(MANUAL_BUNDLES); // Instantiate the asynchronous version of DuckDB-Wasm const worker = new Worker(bundle.mainWorker!); const logger = new duckdb.ConsoleLogger(); const db = new duckdb.AsyncDuckDB(logger, worker); await db.instantiate(bundle.mainModule, bundle.pthreadWorker); ``` -------------------------------- ### Install and Load PostgreSQL Extension Source: https://duckdb.org/docs/current/core_extensions/postgres/overview.html Installs and loads the PostgreSQL extension for DuckDB. This is typically autoloaded but can be managed manually. ```sql INSTALL postgres; LOAD postgres; ``` -------------------------------- ### Example of source_queries parameter Source: https://duckdb.org/docs/current/core_extensions/odbc/functions.html Demonstrates how to use the `source_queries` parameter to execute multiple SQL queries, where the last query's result set is used. ```sql source_queries=[ 'CREATE SECRET s (TYPE s3 [...])', 'FROM nl_train_stations' ], ``` -------------------------------- ### Install and Load Vortex Extension Source: https://duckdb.org/docs/current/core_extensions/vortex.html Use these SQL commands to install and load the Vortex extension into your DuckDB environment. ```sql INSTALL vortex; LOAD vortex; ``` -------------------------------- ### Get Basic Information Source: https://duckdb.org/docs/current/clients/node_neo/overview.html Import the library and log the DuckDB version and configuration option descriptions. ```javascript import duckdb from '@duckdb/node-api'; console.log(duckdb.version()); console.log(duckdb.configurationOptionDescriptions()); ``` -------------------------------- ### Install and Load TPC-DS Extension Source: https://duckdb.org/docs/current/core_extensions/tpcds.html Manually install and load the tpcds extension. This is an alternative to the automatic autoloading feature. ```sql INSTALL tpcds; LOAD tpcds; ``` -------------------------------- ### Create and Populate a New SQLite Database Source: https://duckdb.org/docs/current/core_extensions/sqlite.html This snippet demonstrates creating a new SQLite database, defining a table with specific columns, and inserting a record into that table using SQL. ```sql ATTACH 'new_sqlite_database.db' AS sqlite_db (TYPE sqlite); CREATE TABLE sqlite_db.tbl (id INTEGER, name VARCHAR); INSERT INTO sqlite_db.tbl VALUES (42, 'DuckDB'); ``` -------------------------------- ### Attach to MySQL and Create Table Source: https://duckdb.org/docs/current/core_extensions/mysql.html Connect to a MySQL database and create a new table. This is the initial setup for data manipulation. ```sql ATTACH 'host=localhost user=root port=0 database=mysqlscanner' AS mysql_db (TYPE mysql); CREATE TABLE mysql_db.tbl (id INTEGER, name VARCHAR); INSERT INTO mysql_db.tbl VALUES (42, 'DuckDB'); ``` -------------------------------- ### Enable Logging to File with Shorthand Source: https://duckdb.org/docs/current/operations_manual/logging/overview.html A simplified way to enable file logging by directly providing the storage path. This is equivalent to using `storage_config` with a 'path' key. ```sql CALL enable_logging(storage_path = 'path/to/store/logs'); ``` -------------------------------- ### Install unixODBC on Debian-based Systems Source: https://duckdb.org/docs/current/clients/odbc/linux.html Installs the unixODBC driver manager and odbcinst utility on Debian-based Linux distributions. ```bash sudo apt-get install unixodbc odbcinst ``` -------------------------------- ### Install ccache on Ubuntu Source: https://duckdb.org/docs/current/dev/building/python.html Installs the ccache compiler cache utility on Ubuntu systems. This can speed up C++ compilation times. ```bash sudo apt-get update sudo apt-get install ccache ``` -------------------------------- ### Install Spatial Extension Source: https://duckdb.org/docs/current/core_extensions/spatial/overview.html Use this command to install the spatial extension in DuckDB. This is a prerequisite for loading and using spatial functionalities. ```sql INSTALL spatial; ``` -------------------------------- ### Start UI from Command Line Source: https://duckdb.org/docs/current/core_extensions/ui.html Use this command to launch the DuckDB UI directly from your terminal. ```bash duckdb -ui ``` -------------------------------- ### Read-Only Queriers Example Source: https://duckdb.org/docs/current/quack/security.html Sets up a read-only access control mechanism using SQL macros. Requires a client token for authentication and restricts queries to SELECT statements. ```sql CREATE TABLE quack_tokens (auth_token VARCHAR, user_name VARCHAR); INSERT INTO quack_tokens VALUES ('analytics-team-token', 'analytics'); CREATE MACRO check_token(sid, client_token, server_token) AS ( EXISTS (SELECT 1 FROM quack_tokens WHERE auth_token = client_token) ); CREATE MACRO read_only(sid, query) AS ( regexp_matches(upper(trim(query)), '^(SELECT|FROM|WITH|EXPLAIN)\b') ); CALL quack_serve('quack:localhost', token => 'analytics-team-token'); SET GLOBAL quack_authentication_function = 'check_token'; SET GLOBAL quack_authorization_function = 'read_only'; ``` -------------------------------- ### Install and Load Excel Extension Source: https://duckdb.org/docs/current/core_extensions/excel.html Installs and loads the Excel extension for DuckDB. This extension is typically autoloaded on first use. ```sql INSTALL excel; LOAD excel; ``` -------------------------------- ### Execute SQL Queries and Handle Results in C Source: https://duckdb.org/docs/current/clients/c/query Demonstrates creating a table, inserting data, querying it, and handling the result using `duckdb_query`. Remember to destroy the result object after use. ```c duckdb_state state; duckdb_result result; // create a table state = duckdb_query(con, "CREATE TABLE integers (i INTEGER, j INTEGER);", NULL); if (state == DuckDBError) { // handle error } // insert three rows into the table state = duckdb_query(con, "INSERT INTO integers VALUES (3, 4), (5, 6), (7, NULL);", NULL); if (state == DuckDBError) { // handle error } // query rows again state = duckdb_query(con, "SELECT * FROM integers", &result); if (state == DuckDBError) { // handle error } // handle the result // ... // destroy the result after we are done with it duckdb_destroy_result(&result); ``` -------------------------------- ### Get Profiling Information Source: https://duckdb.org/docs/current/clients/c/api.html Retrieve profiling data for a given connection. Access specific values by key or get all metrics. ```c duckdb_profiling_info duckdb_get_profiling_info(duckdb_connection connection); duckdb_value duckdb_profiling_info_get_value(duckdb_profiling_info info, const char *key); duckdb_value duckdb_profiling_info_get_metrics(duckdb_profiling_info info); ``` -------------------------------- ### Install Specific DuckDB Version on Linux/macOS Source: https://duckdb.org/docs/current/operations_manual/installing_duckdb/install_script Use the DUCKDB_VERSION variable to install a specific past release of the DuckDB CLI client. ```bash curl https://install.duckdb.org | DUCKDB_VERSION=1.2.2 sh ``` -------------------------------- ### Syntactic Sugar for File Logging Configuration Source: https://duckdb.org/docs/current/operations_manual/logging/overview.html Illustrates different ways to invoke `enable_logging` for file storage, showcasing syntactic sugar for common parameters like `storage_path` and omitting the `storage` parameter when it can be inferred. ```sql -- regular invocation CALL enable_logging(storage = 'file', storage_config = {'path': 'path/to/store/logs'}); -- using shorthand for common path storage config param CALL enable_logging(storage = 'file', storage_path = 'path/to/store/logs'); -- omitting `storage = 'file'` -> is implied from presence of `storage_config` CALL enable_logging(storage_config = {'path': 'path/to/store/logs'}); ``` -------------------------------- ### Prepare and Execute Statement Source: https://duckdb.org/docs/current/clients/cli/overview.html Demonstrates the syntax for creating and executing a prepared statement within the DuckDB CLI. ```sql PREPARE my_stmt AS SELECT 1; EXECUTE my_stmt; ``` -------------------------------- ### Install Arch Linux CLI Client Source: https://duckdb.org/docs/current/dev/building/linux.html Installs the DuckDB CLI client directly from Arch's Extra package repository. ```bash sudo pacman -S duckdb ``` -------------------------------- ### Install DuckDB Python Client in Termux Source: https://duckdb.org/docs/current/dev/building/android.html Install the pre-release version of the DuckDB Python client using pip within Termux. ```bash pip install --pre --upgrade duckdb ``` -------------------------------- ### Basic CLI Usage Source: https://duckdb.org/docs/current/clients/cli/overview.html This shows the general syntax for running the DuckDB CLI with options and a database filename. ```bash duckdb ``` -------------------------------- ### Load and Restart Persistent Database for Testing Source: https://duckdb.org/docs/current/dev/sqllogictest/persistent_testing Initiate a persistent database from disk using `load` and trigger a reload with `restart` for testing scenarios that require persistent storage. ```sql # load the DB from disk load __TEST_DIR__/storage_scan.db statement ok CREATE TABLE test (a INTEGER); statement ok INSERT INTO test VALUES (11), (12), (13), (14), (15), (NULL) # ... restart query I SELECT * FROM test ORDER BY a ---- NULL 11 12 13 14 15 ``` -------------------------------- ### Example: Polygonize a Linestring Source: https://duckdb.org/docs/current/core_extensions/spatial/functions.html Demonstrates creating a polygon from a closed linestring ring using ST_Polygonize. ```sql -- Create a polygon from a closed linestring ring SELECT ST_Polygonize([ ST_GeomFromText('LINESTRING(0 0, 0 10, 10 10, 10 0, 0 0)') ]); --- GEOMETRYCOLLECTION (POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0))) ``` -------------------------------- ### Install and Load Encodings Extension Source: https://duckdb.org/docs/current/core_extensions/encodings.html Use these SQL commands to install and load the encodings extension in DuckDB. This is required before using its features. ```sql INSTALL encodings; LOAD encodings; ```