
n8n Postgres Bulk Insert: COPY vs Query Batching
The fastest way to bulk insert into Postgres from n8n is now built into the Postgres node: on n8n 3.4 or later, open the Insert operation's Options and turn on Load Rows With COPY. In my tests it loaded 25,000 rows in 0.48 seconds. The default Single Query mode took 1.13 seconds and Transaction mode took 4.2. But the option changes more than speed. The node outputs one item instead of one per row, Skip on Conflict disappears, and if your items don't all have the same keys, the rows land in a different order than they arrived.
What Does Load Rows With COPY Do in n8n?
It swaps the INSERT statements for PostgreSQL's COPY FROM STDIN. n8n streams every item as one line of COPY text over the same connection, so the server parses a single statement no matter how many rows you send. With statement logging turned on, a 10-item run showed exactly one line: COPY "public"."t01"("email","name") FROM STDIN.
It arrived in PR #44344 and shipped in n8n 3.4.0 on November 24. If you run the n8nio/n8n:latest or :stableimage, you don't have it yet. This morning both tags point to 2.49.9, and that build has no COPY helper in the Postgres node at all. You need a 3.4.x tag, which also means going through the n8n 3.0 breaking changes if you haven't yet. The Postgres node docs page doesn't mention the option either.
Before this, the obvious workaround was a COPY statement in Execute Query, and the community forum has threads where it failed. It can't work, because COPY ... FROM '/file' needs the file on the database server and \copy is a psql command, not SQL.
How Much Faster Is COPY Than Query Batching?
I ran the same workflow (Code node generating N leads, then Postgres Insert into a table with a serial key, a unique email, an array, a jsonb column and two defaults) through every mode. These are medians of three runs of the node's own execution time, with n8n and Postgres 18 on the same Docker network:
| Rows | Single Query | Transaction | Independent | COPY |
|---|---|---|---|---|
| 1,000 | 46 ms | 152 ms | 322 ms | 42 ms |
| 10,000 | 535 ms | 1,702 ms | 3,602 ms | 286 ms |
| 25,000 | 1,134 ms | 4,216 ms | not run | 484 ms |
| 100,000* | 6,695 ms | not run | not run | 2,772 ms |
* 100,000 rows: mean of two runs.
COPY was 2.3x faster than the default at 25,000 rows, 2.4x at 100,000, and 8.7x faster than Transaction. At 1,000 rows it was a tie with Single Query. That surprised me a little, until I read the statement log. Single Query isn't one INSERT per round trip. n8n glues every INSERT into one long multi-statement string and sends it once. Transaction and Independent send one statement per item and wait for each answer.
That waiting is what kills you on a real network. Your n8n box and your database are rarely on the same Docker bridge. So I added 20 ms of delay to the Postgres container with tc netem, about what you'd see from a VPS to a managed database in another region, and ran it again (median of two runs, except where noted):
| Rows | Single Query | Transaction | Independent | COPY |
|---|---|---|---|---|
| 1,000 | 233 ms | 44,432 ms | 45,401 ms | 123 ms |
| 10,000 | 705 ms | 438,522 ms (1 run) | not run | 383 ms |
Transaction and Independent fell off a cliff: 1,000 rows took about 44 seconds, or 44 ms per row against a 21 ms round trip. COPY and Single Query only pay the latency a handful of times per run, so 1,000 rows still took 0.12 and 0.23 seconds. COPY stayed ahead at 10,000 rows, 383 ms against 705 ms. Transaction needed 7 minutes and 18 seconds for the same 10,000 rows, against 0.38 seconds for COPY. If you picked Transaction mode years ago because it sounded safer, this is the single biggest speedup in this post, and you get most of it just by switching back to the default.
What Happens When One Row Fails?
COPY is all or nothing: one bad row and the table gets zero new rows. That sounds scary, so I checked what the other modes do with the same bad data. Ten items, item 6 reusing the email of item 2, unique constraint on email:
| Mode | Rows left in table | What n8n gives you |
|---|---|---|
| COPY | 0 of 10 | One error item paired to item 6, with the real message |
| Single Query (default) | 0 of 10 | Error paired to item 0, description just says 'Failed query' |
| Transaction | 0 of 10 | Workflow stops on the duplicate |
| Independent | 6 of 10 | Workflow stops, first 6 rows stay committed |
| Independent + Continue On Fail | 9 of 10 | 9 rows plus 1 error item |
The default mode was already all or nothing. PostgreSQL runs a multi-statement string as one implicit transaction, so Single Query rolled back too. The real difference is the error. With Continue On Fail, COPY returned duplicate key value violates unique constraint "t02_email_key" paired to item 6, the actual culprit. Single Query paired its error to item 0. With a bad integer ("seven" in item 2), COPY again named item 2 and Single Query named item 0.
Two cases where COPY can't point at a row: a foreign key violation came back paired to all six items, because Postgres checks those at the end of the statement. A BEFORE INSERT trigger that raised an exception was paired correctly to item 4. And a field with no matching column (phone) fails before anything is sent, in every mode.
Why Did COPY Insert My Rows Out of Order?
Because one COPY statement has one column list. When items have different keys, n8n groups them by their sorted key set and runs one COPY per group inside a BEGIN/COMMIT. I sent six items with three shapes. Here's which serial ID each item got:
| Serial id | Input item | Keys on that item |
|---|---|---|
| 1 | item 0 | email, name |
| 2 | item 3 | email, name |
| 3 | item 1 | email, name, score |
| 4 | item 5 | email, name, score |
| 5 | item 2 | |
| 6 | item 4 | email, name, score, source |
Single Query with the same six items gave IDs 1 to 6 in item order. If anything downstream sorts by ID or created_at(an activity feed, a queue, "latest lead"), that matters. The fix is boring: give every item the same keys before the insert.
// Code node: give every item the same keys before COPY
const COLS = ["email", "name", "score", "source"];
return $input.all().map(({ json }) => ({
json: Object.fromEntries(COLS.map((c) => [c, json[c] ?? null])),
}));One catch. A missing key and a nullaren't the same thing. When a key is missing, the column isn't in the COPY column list, so the column default applies (my source column got 'n8n'). An explicit null stores NULL. Single Query behaves the same way, so pick the default you want and fill it in the Code node.
How Do I Upsert or Skip Duplicates With COPY?
COPY has no ON CONFLICT, so the node hides Skip on Conflict, Output Columns and Query Batching when you turn it on. For lead imports, where duplicates are normal, I load into a staging table and let one SQL statement do the merge:
-- once
CREATE UNLOGGED TABLE leads_stage (
batch_id text NOT NULL,
email text,
name text
);Add batch_id: $execution.id to every item, COPY them into leads_stage, then run this in an Execute Query node right after:
WITH moved AS (
INSERT INTO leads (email, name)
SELECT email, name FROM leads_stage
WHERE batch_id = '{{ $execution.id }}'
ON CONFLICT (email) DO NOTHING
RETURNING id
), cleared AS (
DELETE FROM leads_stage
WHERE batch_id = '{{ $execution.id }}'
RETURNING 1
)
SELECT (SELECT count(*) FROM moved) AS inserted,
(SELECT count(*) FROM cleared) AS staged;I tested it with ten staged leads, three of which already existed. It returned { inserted: "7", staged: "10" } (the counts come back as strings because the node outputs big integers as text by default), and the staging table was empty afterwards. Don't try a TEMPtable here. Each Postgres node borrows a connection from the pool, so the next node can land on a different session and won't see it. The batch_idkeeps two executions that overlap from merging each other's rows, which matters if you've read my post on duplicate n8n executions.
Does COPY Store the Same Values as INSERT?
Yes, in everything I threw at it. COPY's text format treats tabs, newlines and backslashes as special, so I wrote seven nasty rows: a tab inside text, \r\n line breaks, a Windows path, the literal string \N(COPY's null marker), an empty string, emoji and curly quotes, nested jsonb, ISO timestamps with and without offsets, and arrays with commas and quotes. Then I inserted the same items with COPY and with Single Query and compared the two tables column by column. All seven rows were identical.
I found one difference, and it favors COPY. A JavaScript array going into a jsonb column ([1, "two", null]) fails in Single Query mode, because the value gets formatted as a Postgres array[...] and the insert dies with invalid input syntax for type integer: "two". COPY serializes it as JSON and stores it fine.
And one bug both modes share: a backslash inside a text[] element ("a\b") came out as abeither way. That's array-literal escaping and it's older than COPY. If you store Windows paths in arrays, check them.
When Should You Leave COPY Off?
- You need the inserted rows back. COPY has no
RETURNING. The output is one item,{ rows: N }. If a later node needs each new ID, generate UUIDs in n8n before the insert or stay on Single Query. - You want the good rows in and the bad rows reported.That's Independent mode with Continue On Fail. It's the slowest mode by far, but it's the only one that keeps 9 of 10.
- Batches are small.At 1,000 rows on a fast network, COPY and Single Query finished within a few milliseconds of each other. Turning it on won't hurt, but it won't fix a slow workflow either.
- Your target relies on rules or is a view.PostgreSQL doesn't fire rules on COPY, and it only copies into a view that has an
INSTEAD OF INSERTtrigger. Row triggers on tables do fire, as my trigger test showed.
For most imports (CSV dumps, CRM syncs, nightly analytics loads into a Supabase lead database), turn it on, normalize the keys and use the staging pattern for duplicates. And if the data only lives inside n8n, compare n8n Data Tables vs Postgres before you pick a database at all.
Key Takeaways
- Load Rows With COPY is in n8n 3.4.0 and later, not in the 2.49.9 build that
latestandstablepoint to today. - It's 2x faster than the defaultat 10,000 to 25,000 rows, and far faster than Transaction or Independent mode once there's network latency.
- The default Single Query mode was already all or nothing, so switching from it doesn't make failures worse. It makes error messages better.
- Normalize item keys or COPY splits the batch and reorders the rows.
- Use a staging table plus
ON CONFLICTto replace Skip on Conflict.
How I Tested This
November 30, 2026, on a Mac mini in Allen, TX. Throwaway containers only: n8nio/n8n:3.4.1 (Postgres node v2.9) and postgres:18 (18.4) on a private Docker network. Every scenario was a separate workflow run with n8n execute-batch, and timings are the Insert node's own executionTimefrom the execution data, so they leave out n8n's startup. The remote numbers used tc qdisc ... netem delay 20ms on the database container, which measured about 21 ms round trip with ping. Row counts and IDs were checked with psql, and statement counts come from log_statement = all. One machine and a small Docker VM, so read the ratios, not the absolute milliseconds.
Got an n8n Workflow That Crawls Through a Big Import?
I build and fix production n8n pipelines that move a lot of rows: CRM syncs, lead imports, ad data into Postgres and Supabase. If yours times out or takes minutes per run, send me the workflow and I'll tell you where the time goes.
Related Posts
n8n
n8n Docker latest Is Now 3.x: Pin It or Roll Back
n8nio/n8n:latest moved to n8n 3.4.3 on Dec 3, 2026. I upgraded a 2.x volume, found a Code node that now dies at 60 seconds and blocked community installs, and rolled it back with db:revert.
n8n
n8n AI Agent Memory Drops Tool Calls: Fixed in 2.49.0
I tested n8n 2.48.4 vs 2.49.0: the AI Agent saved only its last tool round to memory (5/5 repeat tool calls on the next turn) and mixed tool history across items until runs hit Max iterations.
n8n
n8n SSRF Protection: What It Blocks and How to Allow It
I sent 15 URLs through n8n 2.47.7 and 3.2.2 with SSRF protection off and on. The block list caught DNS and IP-encoding tricks, 3.x now blocks Tailscale, AI nodes hide the SSRF error, and dropping the default keyword reopens localhost.