YugabyteDB Database Python Docs | dltHub

Build a YugabyteDB-to-database pipeline in Python using dlt with AI harness support for Claude Code, Cursor, and Codex.

Last updated:

YugabyteDB is a cloud-native distributed database compatible with PostgreSQL and providing a PostgreSQL-compatible YSQL interface. dlt connects to it through SQLAlchemy with the yugabytedb+psycopg2 driver from the sqlalchemy-yugabytedb package, reads the table schemas from the database, and loads the tables you choose into any destination.

dlt is an open-source Python library that handles schema inference, incremental loading, and schema evolution automatically. dlthub provides AI context files that enable code assistants to generate production-ready pipelines. Install with uv add "dlt[hub,sql_database]" and start loading YugabyteDB data in under 10 minutes.


How do I choose what to load from YugabyteDB?

The sql_database source reads the schema of each table you name and turns it into a resource, so the pipeline works with whatever tables your database holds. These options narrow a load down to what you need:

To loadUse
A single tablesql_table(table="orders")
A specific schemasql_database(schema="public")
Views as well as tablessql_database(include_views=True)
Only some columnssql_table(table="orders", included_columns=["id", "status"])
Only some rowssql_database(query_adapter_callback=...)

Always pass table_names=[...] to sql_database(): without it, dlt reflects every table in the schema before loading anything.


How do I connect to the YugabyteDB database?

dlt needs a database user with read access to the tables you want to load. Add its connection details to .dlt/secrets.toml:

[sources.sql_database.credentials] drivername = "yugabytedb+psycopg2" host = "localhost" port = 5433 username = "my_user" password = "my_password" database = "my_database"

Or as a single connection string:

[sources.sql_database] credentials = "yugabytedb+psycopg2://my_user:my_password@localhost:5433/my_database"

Use the sqlalchemy-yugabytedb dialect with the psycopg2-yugabytedb driver or psycopg2; the dialect documents yugabytedb:// and yugabytedb+psycopg2:// connection URLs. The driver must be installed separately.

dlt reads this automatically at runtime — never hardcode credentials in your pipeline script. For production environments, see setting up credentials with dlt for environment variable and vault-based options.


How do I set up and run the pipeline?

Set up a virtual environment and install dlt:

uv init uv add "dlt[hub,sql_database]" sqlalchemy-yugabytedb

1. Install the dlt AI harness:

uv run dlthub ai init --agent <your-agent> # <agent>: claude | cursor | codex

This installs project rules, a secrets management skill, appropriate ignore files, and configures the dlt MCP server for your agent. Learn more →

2. Install the sql-database-pipeline toolkit:

uv run dlthub ai toolkit install sql-database-pipeline

This loads the skills and context about dlt the agent uses to connect to the database, choose tables, and build the pipeline safely. The agent uses MCP tools to inspect credentials — it never needs to read your secrets.toml directly. Learn more →

3. Start LLM-assisted coding:

Use /find-source to load tables from my YugabyteDB database into DuckDB.

The sql-database-pipeline toolkit takes over from here — it checks whether a specialised source fits better, lists the schemas and tables in your database, asks which tables to load and whether you need normalization, then scaffolds a one-table pipeline and runs it with a load limit before scaling up.

4. Run the pipeline:

uv run python sql_database_pipeline.py

If everything is configured correctly, you'll see output like this:

Pipeline yugabytedb_pipeline load step completed in 0.26 seconds 1 load package(s) were loaded to destination duckdb and into dataset yugabytedb_data The duckdb destination used duckdb:/yugabytedb_pipeline.duckdb location to store data Load package 1749667187.541553 is LOADED and contains no failed jobs

Inspect your pipeline and data:

uv run dlthub show

This opens the Pipeline Dashboard where you can verify pipeline state, load metrics, schema (tables, columns, types), and query the loaded data directly.


Python pipeline example

This example loads the customers and orders tables from the public schema into DuckDB. Replace the table names with your own:

import dlt from dlt.sources.sql_database import sql_database def load_tables() -> None: source = sql_database( schema="public", table_names=["customers", "orders"], ) pipeline = dlt.pipeline( pipeline_name="yugabytedb_pipeline", destination="duckdb", dataset_name="yugabytedb_data", ) load_info = pipeline.run(source, write_disposition="replace") print(load_info) if __name__ == "__main__": load_tables()

Credentials are not passed in the code: sql_database() reads them from [sources.sql_database] in .dlt/secrets.toml.

Load only new and changed rows:

Reloading a large table on every run is rarely what you want. Give each table a cursor column and a primary key, and dlt loads only rows changed since the last run and merges them into the destination:

source.orders.apply_hints( incremental=dlt.sources.incremental("updated_at"), write_disposition="merge", primary_key="id", )

Use a timestamp such as updated_at for tables whose rows change, and an increasing id for append-only tables. The toolkit's /adjust-table skill sets this up for you.


Which backend should I use?

The backend decides how rows are read from YugabyteDB. The default, sqlalchemy, works everywhere but is the slowest; switch once the pipeline works:

Data sizeBackendWhy
Under 100k rowssqlalchemy (default)Works with every destination, no extra dependencies
100k – 10M rowspyarrow20–30× faster, keeps decimal and date types exact; needs numpy
Over 10M rowspyarrowconnectorx does not support YugabyteDB
source = sql_database( schema="public", table_names=["customers", "orders"], backend="pyarrow", chunk_size=50000, )

pyarrow, pandas and connectorx skip dlt's row-by-row normalization. The toolkit's /optimize-sql-performance skill measures where time goes and picks a backend, chunk size and parallelism for you. See configuring the backend.


How do I query the loaded data?

Once the pipeline runs, dlt creates one table per source table. You can query with Python or SQL.

Python (pandas DataFrame):

import dlt data = dlt.pipeline("yugabytedb_pipeline").dataset() orders_df = data.orders.df() print(orders_df.head())

SQL (DuckDB example):

SELECT * FROM yugabytedb_data.orders LIMIT 10;

In a marimo or Jupyter notebook:

import dlt data = dlt.pipeline("yugabytedb_pipeline").dataset() data.orders.df().head()

See how to explore your data in marimo Notebooks and how to query your data in Python with dataset.


What destinations can I load YugabyteDB data to?

dlt supports loading into any of these destinations — only the destination parameter changes:

DestinationExample value
DuckDB (local, default)"duckdb"
PostgreSQL"postgres"
BigQuery"bigquery"
Snowflake"snowflake"
Redshift"redshift"
Databricks"databricks"
Filesystem (S3, GCS, Azure)"filesystem"

Change the destination in dlt.pipeline(destination="snowflake") and add credentials in .dlt/secrets.toml. See the full destinations list.


Next steps

Continue your data engineering journey with the other toolkits of the dltHub AI harness:

  • data-exploration — Build custom notebooks, charts, and dashboards for deeper analysis with marimo notebooks.
  • dlthub-platform — Deploy, schedule, and monitor your pipeline in production.
uv run dlthub ai toolkit install data-exploration uv run dlthub ai toolkit install dlthub-platform

Was this page helpful?

Community Hub

Need more dlt context for YugabyteDB?

Request dlt skills, commands, AGENT.md files, and AI-native context.