Skip to main content

polars-db

CI Python 3.11+ codecov License: MIT

A Python library that lets you query relational databases using Polars syntax. Write expressions like pdb.col("age") > 30 and polars-db translates them into the appropriate SQL dialect for your backend.

Motivation

In Python, there is no standard way to manipulate database tables using DataFrame syntax. You either write raw SQL strings or learn a library-specific API like SQLAlchemy or Ibis — none of which feel like the DataFrame code you already write. R solved this years ago with dbplyr, which lets dplyr code run transparently against databases. polars-db brings the same idea to the Polars ecosystem.

Library API style Target audience
SQLAlchemy SQLAlchemy's own API General Python → SQL
Ibis Ibis's own API General DataFrame → SQL
SQLFrame PySpark API PySpark users → SQL
polars-db Polars API Polars users → SQL

Zero learning cost — If you already know Polars, you can query databases immediately. No new API to learn.

Type-safe unified pipelines — Local processing (Polars) and database queries (polars-db) share the same API, eliminating the implicit type coercion, NaN/None confusion, and dtype mismatches that plague pandas-based workflows.

Installation

pip install polars-db

DuckDB works out of the box. For other databases, install the corresponding extras:

# PostgreSQL
pip install polars-db[postgres]

# SQLite
pip install polars-db[sqlite]

# MySQL
pip install polars-db[mysql]

# SQL Server
pip install polars-db[sqlserver]

# BigQuery
pip install polars-db[bigquery]

# Multiple backends at once
pip install polars-db[postgres,mysql]

Supported Databases

Database Extras Driver Connection string example
DuckDB (none) duckdb duckdb:///:memory:
SQLite sqlite adbc-driver-sqlite sqlite:///path/to/db.sqlite
PostgreSQL postgres adbc-driver-postgresql postgresql://user:pass@host:5432/dbname
MySQL mysql PyMySQL mysql://user:pass@host:3306/dbname
SQL Server sqlserver pymssql mssql://user:pass@host:1433/dbname
BigQuery bigquery google-cloud-bigquery bigquery://project/dataset

Usage

Connecting

import polars_db as pdb

conn = pdb.connect("postgresql://user:pass@localhost:5432/mydb")

SELECT Queries

Build queries with the same API as Polars LazyFrame, then call collect() to execute.

# Basic select / filter / sort / limit
df = (
    conn.table("users")
    .filter(pdb.col("age") > 30)
    .select("name", "age")
    .sort("age", descending=True)
    .limit(10)
    .collect()
)
# GROUP BY with aggregation
df = (
    conn.table("sales")
    .group_by("product_id")
    .agg(
        pdb.col("amount").sum().alias("total"),
        pdb.col("id").count().alias("num_sales"),
    )
    .sort("total", descending=True)
    .collect()
)
# JOIN
users = conn.table("users")
orders = conn.table("orders")

df = (
    users.join(orders, on="user_id", how="left")
    .select("name", "amount")
    .collect()
)
# Window functions
df = (
    conn.table("sales")
    .with_columns(
        pdb.col("amount").sum().over("dept").alias("dept_total"),
    )
    .collect()
)
# Cumulative sum with ordering
df = (
    conn.table("sales")
    .with_columns(
        pdb.col("amount")
        .cum_sum()
        .over("dept", order_by="date")
        .alias("running_total"),
    )
    .collect()
)
# CASE WHEN
df = (
    conn.table("users")
    .with_columns(
        pdb.when(pdb.col("age") >= 18)
        .then(pdb.lit("adult"))
        .otherwise(pdb.lit("minor"))
        .alias("category"),
    )
    .collect()
)

Inspecting Generated SQL

query = conn.table("users").filter(pdb.col("age") > 30)
print(query.show_query())
# SELECT * FROM users WHERE age > 30

DDL / Raw SQL

The query builder focuses on SELECT statements. For DDL (CREATE TABLE, etc.) and DML (INSERT, etc.), use execute_raw():

conn.execute_raw("CREATE TABLE users (id INT, name TEXT, age INT)")
conn.execute_raw("INSERT INTO users VALUES (1, 'Alice', 30)")
conn.execute_raw("DROP TABLE IF EXISTS users")

Note: execute_raw() executes SQL as-is. Never pass unsanitized external input via string concatenation.

Development

Prerequisites

  • Python 3.11+
  • uv
  • Docker (for integration tests)

Setup

git clone https://github.com/b-trout/polars-db.git
cd polars-db
uv sync --all-groups --all-extras

Common Commands

This project uses Poe the Poet as a task runner.

# Lint & format
uv run poe lint          # ruff check
uv run poe format        # ruff format
uv run poe type-check    # ty check

# Unit tests (no database required)
uv run poe test-unit

# Integration tests (per backend)
docker compose up -d
POLARS_DB_TEST_BACKEND=postgres uv run poe test-integration

# Run all pre-commit checks (format -> lint -> type-check -> yaml -> docker-lint)
uv run poe pre-commit

License

This project is licensed under the MIT License.

Metadata

Release files for polars-db 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for polars-db 0.1.2
File Size Uploaded Provenance Actions
polars_db-0.1.2.tar.gz 103.0 kB With provenance Details for polars_db-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for polars-db 0.1.2
File Interpreter ABI Platform Actions
polars_db-0.1.2-py3-none-any.whl Python 3 none any Details for polars_db-0.1.2-py3-none-any.whl

Total release size: 152.7 kB

Release files / polars_db-0.1.2.tar.gz

Download URL polars_db-0.1.2.tar.gz
Size 103.0 kB
Tags Source
SHA-256 checksum
How to use checksums
51679d4bdeb0301d68183ba4936e19e317724f31325df03ac0b97752c52ea37a
BLAKE2b-256 checksum
How to use checksums
b2fb40d31f089d4828ca0b65b89a1be86e05479e20a7c4b13343984abf962eaf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 30, 2026.

Transparency log

Release files / polars_db-0.1.2-py3-none-any.whl

Download URL polars_db-0.1.2-py3-none-any.whl
Size 49.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f7207b646b72fcf9666e0f577ebcf410d859f64ea72b4c80202c8f5aa2472475
BLAKE2b-256 checksum
How to use checksums
d013937656417c1aa0c7d34c86250a107428f30abb3960019f8d9fe0879f3a74
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 30, 2026.

Transparency log

Security for polars-db 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for polars_db-0.1.2.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 30, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page