Diagnostic Queries
Symptoms
A string given where a bytea (binary) value was expected was not in a valid hex or escape format. PostgreSQL raises SQLSTATE 22P03 (invalid_binary_representation).
- Malformed hex (
\x…) or escape-format bytea input. - Common with odd-length hex strings or bad escapes.
- Driver/encoding mismatches can cause it.
What the server log shows
ERROR: invalid input syntax for type bytea
Why PostgreSQL raises this — what the manual says
Section 8.4 Binary Data Types:
“Bytea octets are output in hex format by default.”
The bytea input function parses either hex (\x prefix, even number of hex digits) or escape format. Input that violates these rules cannot be decoded to bytes and is rejected with 22P03.
Common causes
- An odd number of hex digits after
\x. - Invalid escape sequences in escape format.
- A driver sending text where binary was expected (or vice versa).
How to fix it
- Use proper hex format:
'\xDEADBEEF'with an even digit count. - Let the driver bind binary parameters rather than building bytea by hand.
- Confirm
bytea_output/client settings match the format you send.
Related & next steps
Reference: PostgreSQL 18 Section 8.4 “Binary Data Types”.
Thanks — noted. This helps keep the database accurate.