Skip to content

Targets

Postgres

Load into Postgres: rows sent by binary COPY into a temporary staging table, then one transaction per commit, into tables SQLLoad creates or tables you already have.

A Postgres target sends each batch by binary COPY into a temporary staging table, on a session of its own per table, then applies the write strategy in one transaction per commit. A failed commit leaves the table as it was. SQLLoad never drops or recreates a table, and loads tables it didn’t create as they are.

A Postgres target uses a kind = "postgres" connection, the same as a Postgres source:

[connections.app_db]
kind = "postgres"
host = "db.example.com"
user = "loader"
password = "${ENV:PGPASSWORD}"
database = "shop"
sslmode = "verify-full"
[targets.app]
connection = "app_db"
loader_schema = "reporting"
Key Meaning
host, user, database Required.
port Default: 5432.
password Use ${ENV:NAME}.
sslmode, sslrootcert, sslcert, sslkey TLS, as libpq takes it. See TLS.

The target’s database overrides the connection’s. sqlload debug shows the target’s user, database, server version and what protects the connection.

The role needs TEMPORARY on the database (every role has it by default), the rights to write the tables it loads, CREATE on the schema for tables SQLLoad creates, and CREATE on the database only when the schema doesn’t exist yet.

Strategy Statement
append INSERT ... SELECT from the staging table.
merge INSERT ... ON CONFLICT (unique_key) DO UPDATE.
delete_insert DELETE of the range it read, then INSERT.
replace DELETE of every row, then INSERT, in one transaction.

replace keeps the table, so its grants, indexes, defaults, triggers and dependent views stay. Readers see the old rows until the commit. --reload is refused.

merge needs a valid, non-deferrable unique index or constraint on exactly the unique_key columns. SQLLoad creates one on tables it creates; on other tables plan fails until one exists. A key repeated within one commit fails that commit.

merge matches a NULL key with a NULL (see NULLs in keys), which ON CONFLICT does only through a unique index created NULLS NOT DISTINCT (Postgres 15 and later). When a key column can be NULL and null_keys is "match":

  • On a new table, SQLLoad creates the index NULLS NOT DISTINCT.
  • On Postgres 14 or earlier, plan refuses the table. Set null_keys = "fail", make the key columns NOT NULL in the source or the table, or upgrade.
  • On an existing table whose unique index treats NULLs as distinct, plan refuses the table and names the statements that replace it. Drop an index that backs a constraint with ALTER TABLE ... DROP CONSTRAINT. Creating the index fails while the table holds copies of a key; see Removing copies of a key.
CREATE UNIQUE INDEX CONCURRENTLY stock_sqlload_key_nulls
ON raw.stock (company, item, site) NULLS NOT DISTINCT;
DROP INDEX raw.stock_sqlload_key;

A key column the table declares NOT NULL, such as a primary key’s, needs nothing more.

replace and delete_insert delete rows, so plan checks foreign keys that reference the table. One with ON DELETE CASCADE, SET NULL or SET DEFAULT fails plan, as it would change another table. Any other one checked immediately gets a warning: the commit fails while referencing rows point at deleted ones. A self-reference doesn’t stop replace.

A table with no _sqlload_load_id column is loaded as it is and never altered:

  • Only the source columns the table has load. plan warns about the others, or fails with on_schema_change = "fail". The table’s other columns get their defaults.
  • No _sqlload_* columns are added.
  • A source column loads into the column of exactly its name; one that matches only when case is ignored fails plan.
  • Values convert as INSERT converts them. A narrower column is allowed (a smaller integer, a shorter varchar(n), a numeric with at least the source’s digits after the point), and a value too large fails the commit; as in any INSERT, text over n only by trailing spaces loses them, and character(n) pads. A column that would round values, such as real, timestamp(3) or a numeric with fewer digits after the point, or one of another kind, fails plan.
Type Postgres
bool boolean
smallint smallint
int bigint
decimal(p,s) numeric(p,s)
float double precision
text text
varchar(n) varchar(n)
date date
time time
timestamp_tz timestamptz ¹
timestamp_ntz timestamp
json jsonb ¹
uuid uuid
binary bytea

¹ Held with a loss: timestamp_tz is a UTC instant without its offset, and jsonb keeps neither key order nor duplicate keys. Postgres arrays load as jsonb.

  • --reload is refused. To add a column to a table that has rows, add it with ALTER TABLE, then empty the table or fill the column, and load again.
  • copy_to_internal_stage is for Snowflake and is refused here.
  • A Postgres CDC source can’t load into a Postgres target.

Container tests against Postgres 17 load into a Postgres target under every write strategy, including replace into a table SQLLoad didn’t create and replace keeping a view, a grant and the primary key, and check the refusals above. Against Postgres 15, a merge on NULL keys keeps one row per key over three loads. Against a TLS-only server, verify-full and require behave as for a source.