Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
D

duckdb-rs

> 数据库
Open source

Ergonomic bindings to duckdb for Rust

944 stars0 likes2 views
WebsiteGitHub

About

Ergonomic bindings to duckdb for Rust

duckdb-rs

duckdb-rs is an ergonomic Rust wrapper for DuckDB.

You can use it to:

  • Query DuckDB with type-safe bindings and an API inspired by rusqlite.
  • Read and write Arrow, Parquet, JSON, and CSV formats natively.
  • Create DuckDB extensions in Rust with custom scalar and table functions.

Quickstart

Create a new project and add the duckdb crate:

bash
cargo new quack-in-rust
cd quack-in-rust
cargo add duckdb -F bundled

Update src/main.rs with the following code:

…

Execute the program with cargo run and watch DuckDB in action!

duckdb-neo: the v2 C API wrapper (experimental)

⚠ DuckDB's v2 C API is in rapid development. Consider duckdb-neo unstable. Feedback is welcome!

duckdb-neo is a second wrapper crate in this repository that targets DuckDB's new v2 C API (duckdb_v2.h) instead of the v1 API used by duckdb. Both crates share the same libduckdb-sys crate and therefore the same bundled DuckDB, build options, and version number.

bash
cargo add duckdb-neo -F bundled
use duckdb_neo::{
    Parameters,
    environment::{Environment, StorageLocation},
};

fn main() -> duckdb_neo::Result {
    let environment = Environment::new()?;
    let database = environment.open(StorageLocation::InMemory)?;
    let connection = database.connect()?;

    let mut result = connection.query("SELECT $1::INTEGER", Parameters::positional(&[&42]))?;
    let chunk = result.next().transpose()?.expect("query returned no rows");
    let values = chunk.get_vector_at::(0)?;
    assert_eq!(values.get(0)?, Some(&42));
    Ok(())
}

libduckdb-sys exposes the v1 bindings as libduckdb_sys::v1 (also re-exported at the crate root) behind the default capi-v1 feature and, behind its capi-v2 feature, the v2 bindings as libduckdb_sys::v2. duckdb-neo disables the default features so only the v2 bindings are generated. duckdb-neo enables that feature itself. Its own capi-v2-p2 feature targets API surface that the bundled DuckDB does not provide yet and does not compile.

Examples

The following examples demonstrate various features and use cases of duckdb-rs:

  • basic - Basic usage including creating tables, inserting data, and querying with and without Arrow.
  • appender - Bulk data insertion using the appender API with transactions.
  • arrow_vtab - Querying an in-memory Arrow RecordBatch from DuckDB SQL (requires the vtab-arrow feature).
  • parquet - Reading Parquet files directly using DuckDB's Parquet extension.
  • vscalar - Defining and registering a custom scalar function (requires the vscalar feature).
  • vtab - Defining a custom table function returning primitive, LIST, and STRUCT columns (requires the vtab feature).
  • repl - Interactive SQL REPL.
  • hello-ext - A DuckDB extension written in Rust (see Building and loading extensions).

Run an executable example using your configured DuckDB library:

bash
cargo run --example basic

Pass each example's required feature when applicable:

bash
cargo run --example arrow_vtab --features vtab-arrow
cargo run --example vscalar --features vscalar
cargo run --example vtab --features vtab
cargo run --example parquet --features parquet

Without bundled, the build links against the pinned prebuilt DuckDB it downloads (or a system library, see Notes on building). To compile DuckDB from source instead, add bundled to the feature list. For example:

bash
cargo run --example basic --features bundled
cargo run --example arrow_vtab --features "bundled vtab-arrow"

hello-ext is a library target rather than an executable, so build it instead of using cargo run:

bash
cargo build --example hello-ext --features loadable-extension

Feature flags

The duckdb crate provides a number of Cargo features that can be enabled to add functionality:

Virtual tables and functions

  • vtab - Base support for creating custom table functions and virtual tables.
  • vtab-arrow - Apache Arrow integration for virtual tables. Enables conversion between Arrow RecordBatch and DuckDB data chunks.
  • vtab-excel - Deprecated no-op retained for feature compatibility.
  • vscalar - Create custom scalar functions that operate on individual values or rows.
  • vscalar-arrow - Arrow-optimized scalar functions for vectorized operations. Requires vscalar.

File formats

  • json - Enables reading and writing JSON files. Implies bundled.
  • parquet - Enables reading and writing Parquet files. Implies bundled.

Bundled DuckDB extensions

These extensions are only available through the CMake build backend and imply bundled-cmake.

  • autocomplete - DuckDB's autocomplete extension.
  • icu - DuckDB's ICU extension for locale-aware operations.
  • tpch - DuckDB's TPC-H benchmark extension.
  • tpcds - DuckDB's TPC-DS benchmark extension.

Data integration

  • appender-arrow - Efficient bulk insertion of Arrow data into DuckDB tables.
  • polars - Integration with Polars DataFrames.
  • rust_decimal - Compatibility impls for rust_decimal::Decimal. DuckDB DECIMAL values use duckdb::types::Decimal as the core full-domain carrier; this feature restores ToSql/FromSql conversions for rust_decimal::Decimal when values fit that crate's decimal domain, including compatibility reads from FLOAT, DOUBLE, and text columns.

Convenience features

  • vtab-full - Enables virtual table features: vtab-arrow and appender-arrow; retains the deprecated vtab-excel compatibility flag.
  • extensions-full - Enables all major extensions: json, parquet, and vtab-full.
  • modern-full - Enables modern Rust ecosystem integrations: chrono, serde_json, url, r2d2, uuid, polars, and rust_decimal.

Build configuration

  • bundled - Uses a bundled version of DuckDB's source code and compiles it during build. This is the simplest way to get started and avoids needing DuckDB system libraries.
  • bundled-cmake - Experimental. Builds DuckDB via its upstream CMake build system instead of cc. Requires a duckdb-rs checkout (not available from crates.io). See step 2 below for details.
  • buildtime_bindgen - Use bindgen at build time to generate fresh bindings instead of using pre-generated ones.
  • capi-v1 (libduckdb-sys only, default) - Generate bindings for the v1 C API header duckdb.h, exposed as libduckdb_sys::v1 and at the crate root. Required by duckdb and by loadable-extension.
  • capi-v2 (libduckdb-sys only) - Also generate bindings for the v2 C API header duckdb_v2.h, exposed as libduckdb_sys::v2. duckdb-neo enables this automatically. With buildtime_bindgen, duckdb_v2.h must sit next to duckdb.h in DUCKDB_INCLUDE_DIR.
  • loadable-extension - Experimental support for creating loadable DuckDB extensions. Includes procedural macros for extension development. Only enable this when building an extension, never for a client application - see Database client or extension?

Installation

Versioning

Starting with DuckDB v1.5.0, the duckdb-rs version encodes the DuckDB version in its second semver component. The format is 1.MAJOR_MINOR_PATCH.x, e.g., DuckDB v1.5.0 maps to duckdb-rs 1.10500.x. Use a tilde requirement to receive duckdb-rs patch releases without automatically moving to a different bundled DuckDB version.

Using stable releases from crates.io

The recommended way to use duckdb-rs is to add it from crates.io:

bash
cargo add duckdb -F bundled

Or manually add it to your Cargo.toml:

toml
[dependencies]
duckdb = { version = "~1.20000.0", features = ["bundled"] }

Using the development version from git

To use the latest development version from the main branch, you can specify a git dependency in your Cargo.toml:

toml
# Use a specific branch
duckdb = { git = "https://github.com/duckdb/duckdb-rs", branch = "main", features = ["bundled"] }

# Use a specific commit
duckdb = { git = "https://github.com/duckdb/duckdb-rs", rev = "abc123def", features = ["bundled"] }

Note: Using the main branch of duckdb-rs means you'll get the latest Rust bindings and features, but you'll still be using whatever version of DuckDB core is bundled with that commit (when using the bundled feature). Use the v1.4-andium branch for LTS releases that stay on DuckDB 1.4 Andium.

Notes on building duckdb and libduckdb-sys

libduckdb-sys is a separate crate from duckdb-rs that provides the Rust declarations for DuckDB's C API. By default, libduckdb-sys attempts to find a DuckDB library that already exists on your system using pkg-config, or a Vcpkg installation for MSVC ABI builds.

You can adjust this behavior in a number of ways:

  1. If you use the bundled feature, libduckdb-sys will use the cc crate to compile DuckDB from source and link against that. This source is embedded in the libduckdb-sys crate and tracks the DuckDB version vendored for that duckdb-rs release. This is probably the simplest solution to any build problems. You can enable this by adding the following in your Cargo.toml file:

    bash
    cargo add duckdb --features bundled

    Cargo.toml will be updated.

    toml
    [dependencies]
    duckdb = { version = "~1.20000.0", features = ["bundled"] }
  2. If you use the bundled-cmake feature, libduckdb-sys will build DuckDB from the local checkout in crates/libduckdb-sys/duckdb-sources using upstream CMake. This keeps plain bundled unchanged while allowing CMake-only extensions such as icu.

    Example:

    toml
    [dependencies]
    duckdb = { git = "https://github.com/duckdb/duckdb-rs", branch = "main", features = ["bundled-cmake", "icu"] }

    Notes:

    • bundled-cmake is experimental and requires a git/workspace checkout. Published crates omit the full duckdb-sources tree.
    • It implies bundled for conditional-compilation gates, but uses CMake instead of the cc backend. Any CMake-only extension feature (e.g. icu) enables it automatically.
    • It always links DuckDB's default static extensions (core_functions and parquet) and therefore implies the parquet Cargo feature.
    • Plain bundled uses DuckDB's standard allocator and skips jemalloc. bundled-cmake enables upstream jemalloc on supported 64-bit, non-musl Linux targets. Other targets follow DuckDB's CMake checks. Set DUCKDB_DISABLE_JEMALLOC=1 to force the standard allocator.
    • Extension autoload/autoinstall are enabled to match bundled, despite upstream CMake defaults. Set DUCKDB_DISABLE_EXTENSION_LOAD=1 or DISABLE_EXTENSION_LOAD=1 to disable external extension install/load support and force autoload/autoinstall off. Statically linked extensions remain available.
    • If ninja is on PATH, the build uses Ninja by default. Set CMAKE_GENERATOR to override.
    • DuckDB builds in Release mode by default, even for Rust debug builds, avoiding DuckDB's much slower debug/sanitizer profile. Override with DUCKDB_CMAKE_BUILD_TYPE or CMAKE_BUILD_TYPE. DUCKDB_CMAKE_BUILD_TYPE takes precedence.
    • DUCKDB_EXTENSION_CONFIGS is unsupported. Setting it fails fast.
    • Use cargo build -vv -F bundled-cmake for CMake configure/build logs.
    • On Windows (MSVC), keep the Cargo target directory short (e.g. CARGO_TARGET_DIR=D:/t). DuckDB's unity build includes sources by a path relative to the build directory, and deep paths exceed MSVC's 260-character limit with C1083. Alternatively set DUCKDB_DISABLE_UNITY=1.
  3. When linking against a DuckDB library already on the system (so not using any of the bundled features)

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rustarrowdatabaseduckdbffi

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category数据库
PricingOpen source

> Related tools

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库