Registry / database / duckdb-engine

duckdb-engine

JSON →
library0.17.0pypypi✓ verified 26d ago

duckdb-engine is an SQLAlchemy driver for DuckDB, a high-performance analytical in-process SQL database system. It enables Python applications to interact with DuckDB databases using SQLAlchemy's ORM and SQL Expression Language. Currently at `v0.17.0`, it receives frequent updates addressing bug fixes and introducing features like filesystem registration.

pip install duckdb-engine
INSTALL
IMPORT
SIG · DUCKDB-ENGINE
D
duckdb-engine
databasepythonv0.17.0
Install
4.3s avg
Import
616ms
Disk
100MB
Pass rate
5/ 10
Env Coverage5 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.17.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
build_error
glibc
py 3.103.95 runs
installs and imports cleanly · install 4.3s · import 0.616s · 101MB
100MB installed
● package 100MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

create_engine
from sqlalchemy import create_engine

This quickstart demonstrates how to create an in-memory DuckDB database using `duckdb-engine` with SQLAlchemy, create a table, insert data, and query it.

from sqlalchemy import create_engine, text # Connect to an in-memory DuckDB database engine = create_engine("duckdb:///:memory:") with engine.connect() as connection: # Execute a simple DDL statement connection.execute(text("CREATE TABLE users (id INTEGER, name VARCHAR)")) connection.commit() # Execute an INSERT statement connection.execute(text("INSERT INTO users (id, name) VALUES (:id, :name)"), {"id": 1, "name": "Alice"}) connection.execute(text("INSERT INTO users (id, name) VALUES (:id, :name)"), {"id": 2, "name": "Bob"}) connection.commit() # Execute a SELECT statement and fetch results result = connection.execute(text("SELECT id, name FROM users")) for row in result: print(f"ID: {row.id}, Name: {row.name}")
Debug
Known issues
breakingPython 3.8 support was dropped in `v0.16.0`. Attempting to use `duckdb-engine` with Python 3.8 will fail.
fix
Upgrade your Python environment to version 3.9 or newer.
affects: <0.16.0
gotchaPrior to `v0.15.1`, there were potential panics in multi-threaded environments. DuckDB generally follows a single-writer, multiple-reader concurrency model.
fix
Ensure `duckdb-engine` is `v0.15.1` or newer. For concurrent operations, particularly writes, ensure each thread uses its own distinct connection (or cursor from a connection pool) to the database.
affects: <0.15.1
gotchaSQLAlchemy's `SERIAL` datatype, typically used for auto-incrementing primary keys in PostgreSQL, is not directly supported by DuckDB, which can lead to issues with ORM-generated schemas.
fix
For auto-incrementing ID columns, explicitly use `sqlalchemy.Sequence()` in your model definitions.
affects: all
gotchaThe `duckdb-engine` dialect is derived from PostgreSQL, and as such, SQLAlchemy may attempt to use PostgreSQL-only features not supported by DuckDB's SQL parser.
fix
Consult the official DuckDB documentation for supported SQL features and adjust your SQLAlchemy ORM or SQL Expression Language constructs accordingly to avoid unsupported syntax.
affects: all
gotchaDivision operations might produce unexpected results due to default casting behavior (`div_is_floordiv=True` by default) prior to `v0.14.1`.
fix
Upgrade to `duckdb-engine` `v0.14.1` or newer, which sets `div_is_floordiv` to `False` by default, ensuring more predictable division casts.
affects: <0.14.1
gotchaConnecting to MotherDuck databases requires specific connection string parameters, including an optional `motherduck_token` for authentication.
fix
Use the URI format `duckdb:///md:<my_database>?motherduck_token=<my_token>`. It is recommended to manage `MOTHERDUCK_TOKEN` as an environment variable rather than hardcoding.
affects: all
Errors
Common errors & fixes
sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:duckdb
This error occurs because either the `duckdb-engine` package (which provides the SQLAlchemy dialect for DuckDB) or its fundamental dependency, the `duckdb` Python package, is not installed or accessible in the current Python environment.
fix
Install both `duckdb` and `duckdb-engine` using pip: `pip install duckdb duckdb-engine`
duckdb.duckdb.CatalogException: Catalog Error: Type with name REGCLASS does not exist!
This error typically arises during SQLAlchemy's schema reflection (e.g., when calling `MetaData.reflect()`) because `duckdb-engine` derives from the PostgreSQL dialect, and DuckDB does not support the `REGCLASS` type that PostgreSQL uses internally for some catalog queries.
fix
This is a known incompatibility. Ensure you are using recent versions of `duckdb-engine` and `duckdb`. If the issue persists with `MetaData.reflect()`, consider selectively reflecting specific tables or columns, or defining your models directly rather than relying solely on reflection for problematic schemas. If it's related to auto-incrementing IDs, explicitly use `sqlalchemy.Sequence` (see the next problem).
duckdb.duckdb.CatalogException: Catalog Error: Type with name SERIAL does not exist!
SQLAlchemy's dialect for `duckdb-engine` inherits from PostgreSQL, which defaults to using the `SERIAL` pseudo-type for auto-incrementing integer primary keys. DuckDB does not natively support the `SERIAL` type, leading to a catalog error during DDL execution.
fix
When defining auto-incrementing primary keys in your SQLAlchemy models or table definitions, explicitly use `sqlalchemy.Sequence` instead of relying on the implicit `SERIAL` type mapping. For example:
```python
from sqlalchemy import Column, Integer, Sequence, create_engine, MetaData, Table

engine = create_engine('duckdb:///:memory:')
metadata = MetaData()

user_id_seq = Sequence('user_id_seq')
users_table = Table(
    'users',
    metadata,
    Column('id', Integer, user_id_seq, server_default=user_id_seq.next_value(), primary_key=True),
    Column('name', String)
)

metadata.create_all(bind=engine)
```
NotImplementedException: Not implemented Error: Can not scan a gcs:// gs:// or r2:// url without a secret providing its endpoint currently. Please create an R2 or GCS secret containing the credentials for this endpoint and try again.
When trying to access cloud storage (like GCS, S3, R2) via `duckdb-engine` (or directly with `duckdb`) using `fsspec` or URL paths, DuckDB's `httpfs` (or specific cloud storage extensions) might require explicit credential configuration (secrets) or the necessary extensions might not be loaded, especially with recent DuckDB versions.
fix
Ensure the required DuckDB extensions (e.g., `httpfs`, `s3`, `azure`) are installed and loaded. For authenticated access to cloud storage, configure secrets using DuckDB's `CREATE SECRET` command or by passing appropriate connection arguments to `create_engine` that allow DuckDB to handle the authentication. For example, to preload an extension and configure a secret for S3:
```python
from sqlalchemy import create_engine

engine = create_engine(
    'duckdb:///:memory:',
    connect_args={
        'preload_extensions': ['httpfs', 's3'],
        'config': {
            's3_region': 'your-s3-region',
            's3_access_key_id': 'YOUR_ACCESS_KEY',
            's3_secret_access_key': 'YOUR_SECRET_KEY'
        }
    }
)
# Alternatively, register fsspec filesystem and then use create secret in SQL for newer DuckDB versions
# import duckdb
# conn = duckdb.connect()
# conn.execute("INSTALL httpfs;")
# conn.execute("LOAD httpfs;")
# conn.execute("CREATE SECRET s3_secret (TYPE S3, KEY_ID 'YOUR_ACCESS_KEY', SECRET 'YOUR_SECRET_KEY', REGION 'your-s3-region');")
```
Upgrade
Version history
0.17.0latest on PyPI · released Mar 29, 2025
Audit
Dependencies
duckdbrequiredProvides the underlying analytical database engine.
SQLAlchemyrequiredThe ORM and SQL Expression Language framework this driver extends.
packagingrequiredRuntime dependency for package metadata handling.
Agent activity
20 hits · last 30 days
node
18
OpenAI (training)
1
Resources