psql COPY FROM STDIN Fix: CVE-2026-6464
Chat2DB TeamCOPY ... FROM STDIN is how most PostgreSQL data gets loaded from scripts: plain-format pg_dump files use it for every table, and psql -c "COPY t FROM STDIN" < data.tsv is a standard ETL one-liner. Two recent psql releases changed how it behaves. The August 2026 update releases (18.6, 17.11, 16.15, 15.19, 14.24) fixed CVE-2026-6464, a flaw that made psql execute COPY data lines as psql commands. That fix had a side effect: psql started rejecting command strings with more than one COPY ... FROM STDIN. The November 12, 2026 releases (18.7, 17.12, 16.16, 15.20, 14.25) fixed the side effect.
This article reproduces both problems with psql 18.4, 18.6 and 18.7, explains what changed, and lists what to check in scripts and pipelines. psql is a client program, so the version that matters is the one installed where your scripts run, not the server version.
CVE-2026-6464: Data Lines Run as Commands
The advisory describes the bug this way: if COPY FROM STDIN or \copy FROM STDIN fails before the server signals that it is ready to receive rows, psql treats the inline data lines as psql commands. A COPY FROM with a file name is not affected. The advisory rates it CVSS 3.0 8.1 (High) and notes that a full attack needs control of both the server (to make the COPY fail early) and the data rows, but that an attacker who controls only the data could still succeed if the COPY happens to fail for some other reason.
The easiest way to make a COPY fail before data transfer starts is to name a table that does not exist. Here is a script where the data block contains a psql meta-command:
COPY missing_table FROM stdin;
1 hello
\! echo INJECTED-FROM-DATA-LINE
\.
SELECT 42 AS after_copy;With psql 18.4 (before the fix), running it with psql -f early.sql:
psql:early.sql:1: ERROR: relation "missing_table" does not exist
INJECTED-FROM-DATA-LINE
psql:early.sql:4: error: invalid command \.
psql:early.sql:5: ERROR: syntax error at or near "1"
LINE 1: 1 hello
^The \! line was data, but psql ran it as a shell command. \! executes anything on the client machine with the privileges of the user running psql. The first data row became the start of a SQL statement, and the \. terminator became an invalid command. The same happened with \copy missing_table from stdin and when the script was piped into psql's standard input.
With psql 18.6 or 18.7 on the same script:
psql:early.sql:1: ERROR: relation "missing_table" does not exist
after_copy
------------
42The fixed psql knows it sent a COPY FROM STDIN and did not get the "ready for data" response, so it reads and discards the data block up to \. and then continues with the next real command. The \! line is never executed.
Where This Matters in Practice
The realistic exposure is restoring SQL text that contains COPY data you did not produce yourself:
- plain-format
pg_dumpoutput from a third party, a customer, or an untrusted backup location, restored withpsql -f; - generated load scripts built from user-supplied CSV or TSV data;
- CI jobs that replay fixtures from pull requests.
If any COPY in such a file fails early (a table that was not created, a missing column, a permission error), the old psql would start executing the data. Since data rows can contain any text, including \! or \o lines, the person who controls the data controls what psql does next.
-v ON_ERROR_STOP=1 helps independently of the fix. With it, psql stops at the first error, before reading the data block:
psql:early.sql:1: ERROR: relation "missing_table" does not existand exits with status 3. Using ON_ERROR_STOP=1 for every non-interactive psql run is good practice anyway, since it also prevents half-applied migrations.
The Side Effect in 18.6: Multiple COPY FROM STDIN in One -c String
To discard data correctly, the fixed psql needs to know how many COPY ... FROM STDIN commands it sent. In 18.6 it counted them wrongly when one -c string held several statements: the scanner stopped at the first semicolon, so it counted only the first COPY. When the server then asked for data for the second COPY, psql treated it as a protocol violation:
printf '1\n\\.\n2\n\\.\n' | psql -c "COPY a FROM STDIN; COPY b FROM STDIN"psql 18.6:
COPY 1
unexpected COPY_IN result, aborting connectionThe exit status was 2 (connection problem), and because a multi-statement -c string runs as a single transaction, the first COPY was rolled back too: both tables were empty afterwards. In the psql source, this message is reached when the server asks for COPY data that psql did not expect; the comment there says such a server is "broken or possibly malicious", which is the right reaction to a real attack but the wrong one for a legitimate script.
psql 18.4 (before the CVE fix) and psql 18.7 both load the data:
COPY 1
COPY 1The 18.7 release notes describe the fix as making sure COPY FROM STDIN "is counted correctly across all commands in a multi-query string". The patch keeps scanning past each semicolon so every COPY in the string is counted.
Client Version, Not Server Version
We also mixed versions. psql 18.7 against an 18.6 server loads both tables; psql 18.6 against an 18.7 server still fails with unexpected COPY_IN result, aborting connection. The behavior follows the psql binary. Check the client that your cron jobs, containers and CI images use:
psql --versionDocker images, CI runners and developer laptops often have a different psql than the database server, and they are updated on a different schedule.
Workarounds if You Are Stuck on 18.6
If you cannot update psql yet, any of these avoids the problem:
- Run each COPY in its own
-coption:psql -c "COPY a FROM STDIN" -c "COPY b FROM STDIN". Each-cis a separate command string. - Put the statements in a file and use
-f, with the data inline after each COPY, which is howpg_dumpwrites it. - Use
\copywith a file name for each table, orCOPY ... FROMa server-side file where permissions allow.
Note that separate -c options run as separate transactions, unlike one multi-statement string; add -1 (--single-transaction) if both loads must succeed or fail together.
What Did Not Change
Errors that happen after data transfer has started were never part of the CVE. A row with too many columns, for example, ends the COPY with an error and psql skips to the \. terminator on every version:
psql:mid.sql:5: ERROR: extra data after last expected column
CONTEXT: COPY a, line 2: "2 extra"The table is left empty because COPY is all-or-nothing. ON_ERROR ignore (PostgreSQL 17 and later) does not change that here: it skips rows whose values fail type conversion, but a row with the wrong number of columns still aborts the whole COPY, which we confirmed on 18.7.
Checklist
- Update psql everywhere it runs: servers, application containers, CI images, bastion hosts, laptops. The fixed and regression-free versions are 18.7, 17.12, 16.16, 15.20 and 14.25 or later. 14.25 is the last PostgreSQL 14 release.
- Treat SQL dumps as code (see the pg_dump and pg_restore guide (opens in a new tab) for the formats). Do not restore plain-text dumps from untrusted sources with psql. Prefer custom-format dumps with
pg_restore, and review plain-text files before running them. - Use
-v ON_ERROR_STOP=1for scripts, so an early failure stops the run instead of continuing into data. - Look for
unexpected COPY_IN resultin job logs from the last three months. It points to a psql 18.6 / 17.11 / 16.15 / 15.19 / 14.24 client running a multi-statement-cstring with several COPYs, and those jobs loaded nothing.
If you load data interactively rather than through scripts, a GUI client avoids psql's inline-data parsing entirely. In Chat2DB (opens in a new tab) you can import CSV files into a PostgreSQL table through the import wizard and check the row counts before committing.
Summary
CVE-2026-6464 made psql execute COPY FROM STDIN data lines as psql commands when the COPY failed before data transfer, which allowed \! shell commands hidden in data to run on the client; psql 18.4 does exactly that, while 18.6 and 18.7 discard the data. The August 2026 fix introduced a regression in which psql rejected command strings containing more than one COPY FROM STDIN with unexpected COPY_IN result, aborting connection, rolling back the whole string. The November 2026 releases fix the counting. Update every psql client to 18.7, 17.12, 16.16, 15.20 or 14.25, run scripts with ON_ERROR_STOP=1, and never restore untrusted plain-text dumps with psql.
