Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-29 · container image postgres:17
22021 means PostgreSQL received bytes that are not valid in the encoding it expected, here UTF8. The hex value at the end is the first bad byte: 0xb0 was the first byte of EUC-KR Korean text, and 0x00 was a NUL byte. For COPY, the CONTEXT line also shows the line number.
The server and client encoding were both UTF8 in these runs. The fixes were to tell PostgreSQL the real encoding of the data, or to remove the NUL byte before loading.
01Loading an EUC-KR (CP949) CSV with COPY
The CSV line 1,가나 was saved in EUC-KR. COPY ... WITH (FORMAT csv) failed with invalid byte sequence for encoding "UTF8": 0xb0 and CONTEXT: COPY customers, line 1. server_encoding and client_encoding were both UTF8.
Adding ENCODING 'EUC_KR' to the COPY options loaded the row, and the name came out as 가나.
Setup
CREATE TABLE lab_a.customers (id int PRIMARY KEY, name text);CREATE TABLECOPY lab_a.customers FROM PROGRAM 'printf "1,\260\241\263\252\n"' WITH (FORMAT csv);ERROR: 22021: invalid byte sequence for encoding "UTF8": 0xb0
CONTEXT: COPY customers, line 1SHOW server_encoding;
SHOW client_encoding; server_encoding
-----------------
UTF8
(1 row)
client_encoding
-----------------
UTF8
(1 row)The ENCODING option describes the file, not the database. The database stays UTF8 and PostgreSQL converts while loading.
COPY lab_a.customers FROM PROGRAM 'printf "1,\260\241\263\252\n"' WITH (FORMAT csv, ENCODING 'EUC_KR');
SELECT * FROM lab_a.customers;COPY 1
id | name
----+------
1 | 가나
(1 row)02convert_from() with the wrong source encoding
A bytea column held \xb0a1b3aa (4 bytes), which is 가나 in EUC-KR. convert_from(raw, 'UTF8') failed with 22021: invalid byte sequence for encoding "UTF8": 0xb0.
convert_from(raw, 'EUC_KR') returned 가나.
Setup
CREATE TABLE legacy_files (id int PRIMARY KEY, raw bytea);
INSERT INTO legacy_files VALUES (1, '\xb0a1b3aa'::bytea);CREATE TABLE
INSERT 0 1SELECT id, convert_from(raw, 'UTF8') FROM legacy_files;ERROR: 22021: invalid byte sequence for encoding "UTF8": 0xb0SELECT id, encode(raw, 'hex') AS raw_hex, length(raw) AS bytes FROM legacy_files; id | raw_hex | bytes
----+----------+-------
1 | b0a1b3aa | 4
(1 row)SELECT id, convert_from(raw, 'EUC_KR') FROM legacy_files; id | convert_from
----+--------------
1 | 가나
(1 row)03A NUL (0x00) byte in the input file
The CSV line had a NUL byte between line1 and line2. COPY failed with invalid byte sequence for encoding "UTF8": 0x00. Looking at the line as bytes showed 00 in the middle (312c6c696e6531006c696e6532). PostgreSQL text values cannot contain NUL.
Piping the file through tr -d "\000" before COPY removed the byte, and the row was stored as line1line2.
Setup
CREATE TABLE lab_a.memos (id int PRIMARY KEY, body text);CREATE TABLECOPY lab_a.memos FROM PROGRAM 'printf "1,line1\000line2\n"' WITH (FORMAT csv);ERROR: 22021: invalid byte sequence for encoding "UTF8": 0x00
CONTEXT: COPY memos, line 1SELECT encode('1,line1\000line2'::bytea, 'hex') AS csv_bytes_hex; csv_bytes_hex
----------------------------
312c6c696e6531006c696e6532
(1 row)COPY lab_a.memos FROM PROGRAM 'printf "1,line1\000line2\n" | tr -d "\000"' WITH (FORMAT csv);
SELECT * FROM lab_a.memos;COPY 1
id | body
----+------------
1 | line1line2
(1 row)FAQ
How do I fix invalid byte sequence for encoding UTF8 in COPY?
Tell COPY the file's real encoding, for example WITH (FORMAT csv, ENCODING 'EUC_KR'). That loaded the Korean CSV in the first case.
What does 0x00 in invalid byte sequence mean?
The data contains a NUL byte, which PostgreSQL text cannot store. Remove it before loading; in the third case tr -d "\000" did that.
How do I find the encoding of my database and client?
Run SHOW server_encoding; and SHOW client_encoding;. Both were UTF8 here, so the problem was the file, not the settings.
convert_from fails with 22021. What should I pass?
The encoding the bytes were actually written in. convert_from(raw, 'EUC_KR') worked where 'UTF8' failed.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12