Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

invalid byte sequence for encoding UTF8 while importing data

ErrorERROR: invalid byte sequence for encoding "UTF8": 0xe9

Short answer

PostgreSQL checks incoming text against the encoding the client declares. If the file is really Latin-1 or Windows-1252 but is read as UTF8, the first byte that is not valid UTF8 stops the load. Tell the server the true encoding, with COPY ... ENCODING or client_encoding.

  • Applies to PostgreSQL 16, PostgreSQL 17, psql 17
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the client encoding in use, and the encoding of the target database. The error line number from COPY shows where the first bad row is.

    psql -d appdb -c "SHOW client_encoding" -c "SHOW server_encoding"
  2. 2Match what you see, then jump to its fix

    1. 1A single byte such as 0xe9 in the message, near accented text in that lineThe file is in a single-byte encoding but loaded as UTF8Fix
    2. 2The same file loads in one shell or tool and fails in anotherclient_encoding is set to something other than the dataFix

What does the error look like?

  • ERROR: invalid byte sequence for encoding "UTF8": 0xe9
  • CONTEXT: COPY customers, line 12

Cause 1 of 2

The file is in a single-byte encoding but loaded as UTF8

1Check

Input is interpreted using the ENCODING option, or the current client encoding when that option is absent. A Latin-1 or Windows-1252 export fails against UTF8.

psql -d appdb -c "SHOW client_encoding"

2Fix

Name the file's real encoding. Server-side COPY and client-side \copy both accept it.

\copy customers FROM 'customers.csv' WITH (FORMAT csv, HEADER true, ENCODING 'WIN1252')

3Verify

The load completes and accented values read back correctly.

psql -d appdb -c "SELECT name FROM customers WHERE name ~ '[^ -~]' LIMIT 5"

Confirmed by postgresql.org

Did this fix it?

Cause 2 of 2

client_encoding is set to something other than the data

1Check

PGCLIENTENCODING, the client_encoding setting or a psql \encoding command picks the client encoding at connect time, and the server converts from it.

echo "PGCLIENTENCODING=$PGCLIENTENCODING"
psql -d appdb -c "SHOW client_encoding"

2Fix

Unset the stray variable, or set the encoding to match the data for this session.

unset PGCLIENTENCODING
psql -d appdb -c "SET client_encoding TO 'UTF8'"

3Verify

SHOW client_encoding reports the value you chose and the import runs.

psql -d appdb -c "SHOW client_encoding"

Confirmed by postgresql.org

Did this fix it?

How do I stop it coming back?

  • Export data as UTF8 from the source system and say so in the export tool.
  • Pass ENCODING on every COPY from a file whose origin you do not control.
  • Avoid SQL_ASCII as a database encoding; it accepts bytes without validating them, so bad data surfaces later.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.