Skip to content

Guides

Load Postgres tables

Load a Postgres database's tables into a warehouse, keep them up to date with cursors, and connect over TLS or through an SSH tunnel.

This guide loads a Postgres database’s tables into a warehouse, keeps them up to date, and connects over TLS or an SSH tunnel. Every setting is on the Postgres source page. To capture changes from a replication slot instead, see Capture Postgres changes.

A source names a connection and the tables to load. Here every table in public loads into DuckDB except the audit tables:

default_target = "local"
[connections.app]
kind = "postgres"
host = "db.example.com"
user = "loader"
password = "${ENV:APP_DB_PASSWORD}"
database = "shop"
[connections.warehouse]
kind = "duckdb"
database = "shop.duckdb"
[targets.local]
connection = "warehouse"
loader_schema = "raw"
[sources.app]
kind = "postgres"
connection = "app"
description = "The shop's application database."
include = ["public.*"]
exclude = ["public.audit_*"]
write_strategy = "replace"
[sources.app.tables."public.customers"]
description = "Customers, one row per customer."
[sources.app.tables."public.orders"]
description = "Orders, one row per order."
write_strategy = "merge"
unique_key = "{primary_key}"
cursor_column = "updated_at"

The user needs SELECT on the tables. For another warehouse, swap the target’s connection, such as Snowflake. Run sqlload plan to see what loads, then sqlload load:

$ sqlload plan -v
…
Loads (2) sqlload load
├── raw.app__customers replace
│ └── source postgres app public.customers
└── raw.app__orders merge (unique_key: order_id ← {primary_key})
├── source postgres app public.orders
└── reads updated_at: first run, everything
…
  • customers takes the source’s replace: each run rebuilds it.
  • orders merges on its primary key, reading from the destination’s maximum updated_at.
  • audit_log matches exclude, so it isn’t listed.

Every table needs a write_strategy, from its entry or its source. See Choosing tables and Write strategies.

Run sqlload load on a schedule. A table with a cursor_column continues from the destination’s maximum of it:

$ sqlload plan -v app__orders
…
├── reads updated_at >= 2026-10-12 16:00:00.000+00 (warehouse maximum)
├── commits about every 1 GB, in updated_at order (an index leads with it, NULLs first), between distinct values
…

For rows that arrive late, re-read a window before the maximum with lookback. delete_insert replaces it and needs no key:

[sources.app.tables."public.page_views"]
description = "Page views, one row per view."
write_strategy = "delete_insert"
cursor_column = "viewed_at"
lookback = "2h"
$ sqlload plan -v app__page_views
…
├── reads viewed_at >= 2026-10-14 19:20:00.000+00 (warehouse maximum minus lookback 2h)
…

To reload a fixed window around today on every run, see Reload a window.

A table whose cursor_column leads an index commits about every 1 GB, as above; any other commits once. For a large first load, set batch_size and cursor_start to commit one window at a time:

[sources.app.tables."public.page_views"]
description = "Page views, one row per view."
write_strategy = "delete_insert"
cursor_column = "viewed_at"
cursor_start = "2026-10-01"
batch_size = "7d"
lookback = "2h"
$ sqlload plan -v app__page_views
…
└── raw.app__page_views delete_insert (cursor: viewed_at)
├── source postgres app public.page_views
├── reads viewed_at >= 2026-10-01 00:00:00.000+00 (first run, from cursor_start)
├── commits 7 windows of 7 days, one commit each
├── route read as Arrow -> DuckDB takes Arrow
└── note windows are read in one REPEATABLE READ snapshot, held open on the source for the whole table load (mind idle_in_transaction_session_timeout and vacuum)
…
$ sqlload load app__page_views
…
loaded raw.app__page_views 2,000 rows 0.1s
…

A failure keeps the windows that committed; the next run carries on. See Batches. To load a range again, give --start-cursor and --end-cursor (not included):

$ sqlload plan -v app__page_views --start-cursor 2026-10-05 --end-cursor 2026-10-07
…
├── reads viewed_at from 2026-10-05 00:00:00.000000+00 (--start-cursor), below 2026-10-07 00:00:00.000000+00 (--end-cursor)
…
$ sqlload load app__page_views --start-cursor 2026-10-05 --end-cursor 2026-10-07
…
loaded raw.app__page_views 288 rows 0.0s
…

Set sslmode on the connection. verify-full encrypts and checks the server’s certificate and host name:

[connections.app]
kind = "postgres"
host = "db.example.com"
port = 5432
user = "loader"
password = "${ENV:APP_DB_PASSWORD}"
database = "shop"
sslmode = "verify-full"
sslrootcert = "certs/db-ca.pem"
# sslcert = "certs/loader.pem" # a client certificate, with sslkey
# sslkey = "certs/loader.key"

Give sslrootcert for a private CA, such as Amazon RDS and Aurora’s or a company’s; without it, SQLLoad trusts only the public CA roots built into it. See TLS for the modes. sqlload debug ends each Postgres source’s line with not encrypted, TLS, certificate not checked, TLS, CA checked or TLS, CA and host name checked:

$ sqlload debug
ok config 2 connections, 1 targets, 1 sources
ok target local DuckDB v2.0.0
ok source app loader@shop, PostgreSQL 17.11 (Debian 17.11-1.pgdg13+2), TLS, CA and host name checked

When the default, prefer, falls back to plain text over the network, plan warns (not for localhost, a loopback address, a Unix socket, or sslmode = "disable"):

$ sqlload plan
…
Warnings (1)
└── [sources.app] reads legacy-db.example.com:5432 in plain text: the server does not offer TLS, so sslmode = "prefer" fell back; set sslmode = "require" (or "verify-full") on connection app to refuse plain text, or "disable" to accept it

A postgresql:// URI for sqlload copy takes them as a query, such as ?sslmode=verify-full&sslrootcert=certs/db-ca.pem.

When the database is reachable only through a bastion host, give the connection an ssh table. host and port are then the database as the bastion sees it:

[connections.app]
kind = "postgres"
host = "db.internal"
port = 5432
user = "loader"
password = "${ENV:APP_DB_PASSWORD}"
database = "shop"
ssh.host = "bastion.example.com"
ssh.user = "deploy"
ssh.private_key_file = "~/.ssh/id_ed25519"

The bastion’s host key must be in known_hosts (~/.ssh/known_hosts by default); an unknown key fails with its fingerprint. Once the bastion’s administrator confirms it, add the key:

ssh-keyscan -p 22 bastion.example.com >> ~/.ssh/known_hosts

sqlload debug says which hop failed: the tunnel, or the database through it. plan shows via ssh deploy@bastion.example.com:22 on each table’s source line. See the ssh keys in sqlload.toml and SSH tunnels.