Skip to main content

LlamaIndex Readers Integration: Database

Overview

Database Reader is a tool designed to query and load data from databases efficiently.

Key features

  • Accepts connection via SQLDatabase, SQLAlchemy Engine, full URI, or discrete credentials
  • Optional schema selection (namespace)
  • Column-level metadata mapping (metadata_cols) and text exclusion (excluded_text_cols)
  • Custom id_ generation function (document_id)
  • Supports streaming (lazy_load_data) and async (aload_data)

Installation

You can install Database Reader via pip:

pip install llama-index-readers-database

Usage

from llama_index.readers.database import DatabaseReader

# Initialize DatabaseReader with the SQL database connection details
reader = DatabaseReader(
    sql_database="<SQLDatabase Object>",  # Optional: SQLDatabase object
    engine="<SQLAlchemy Engine Object>",  # Optional: SQLAlchemy Engine object
    uri="<Connection URI>",  # Optional: Connection URI
    scheme="<Scheme>",  # Optional: Scheme
    host="<Host>",  # Optional: Host
    port="<Port>",  # Optional: Port
    user="<Username>",  # Optional: Username
    password="<Password>",  # Optional: Password
    dbname="<Database Name>",  # Optional: Database Name
)

# Load data from the database using a query
documents = reader.load_data(
    query="<SQL Query>"  # SQL query parameter to filter tables and rows
)
# Initialize DatabaseReader with the SQL connection string and custom database schema
from llama_index.readers.database import DatabaseReader

reader = DatabaseReader(
    uri="postgresql+psycopg2://user:pass@localhost:5432/mydb",
    schema="warehouse",  # optional namespace
)
# Streaming variant, excluded id from text_resource
for doc in reader.lazy_load_data(
    query="SELECT * FROM warehouse.big_table", excluded_text_cols={"id"}
):
    process(doc)

# Async variant, added region to metadata
docs_async = await reader.aload_data(
    query="SELECT * FROM warehouse.big_table", metadata_cols=["region"]
)
# Advanced usage with custom named metadata columns, columns excluded from the `Document.text_resource`, and a dynamic `Document.id_` generated from row data and a fstring template
from llama_index.readers.database import DatabaseReader

reader_media = DatabaseReader(
    uri="postgresql+psycopg2://user:pass@localhost:5432/mydb",
    schema="media",  # optional namespace
)

docs = reader_media.load_data(
    query="SELECT id, title, body, updated_at FROM media.articles",
    metadata_cols=[
        ("id", "article_id"),
        "updated_at",
    ],  # map / include in metadata
    excluded_text_cols=["updated_at"],  # omit from text
    document_id=lambda row: f"media-articles-{row['id']}",  # custom document id
)

This loader is designed to be used as a way to load data into LlamaIndex.

Metadata

Release files for llama-index-readers-database 0.8.0

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

Source distribution (sdist)

Source distribution for llama-index-readers-database 0.8.0
File Size Uploaded Provenance Actions
llama_index_readers_database-0.8.0.tar.gz 6.7 kB Without provenance Details for llama_index_readers_database-0.8.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for llama-index-readers-database 0.8.0
File Interpreter ABI Platform Actions
llama_index_readers_database-0.8.0-py3-none-any.whl Python 3 none any Details for llama_index_readers_database-0.8.0-py3-none-any.whl

Total release size: 13.1 kB

Release files / llama_index_readers_database-0.8.0.tar.gz

Download URL llama_index_readers_database-0.8.0.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
50afa89a8a36d04f7dbd690df642d3b19ec348afb4648ef02334079eb0129892
BLAKE2b-256 checksum
How to use checksums
96615ef0cbeee02433e196a22bd7b018d12620e8f60c47125be663120dbac6b0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.8 {"installer":{"name":"uv","version":"0.13.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / llama_index_readers_database-0.8.0-py3-none-any.whl

Download URL llama_index_readers_database-0.8.0-py3-none-any.whl
Size 6.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c13432466f81c7e0b71cba85aeab68d06f544e04baa651c6ae5962ae304d5aa7
BLAKE2b-256 checksum
How to use checksums
b494cc1fab7965c2f1b30eaa15b4c4ef6d2d9e3aac4c1e98e0cf07816126adca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.8 {"installer":{"name":"uv","version":"0.13.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for llama-index-readers-database 0.8.0

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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