SQLSTATE 22P03 ERROR Class 22: Data Exception

invalid_binary_representation invalid input syntax for type bytea — 22P03

PostgreSQL error "invalid input syntax for type bytea" (SQLSTATE 22P03): what it means, common causes, and how to fix it.

PG 12, 13, 14, 15, 16, 17, 18 Official docs
Last reviewed May 2025 Grounded in source

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

  1. Use proper hex format: '\xDEADBEEF' with an even digit count.
  2. Let the driver bind binary parameters rather than building bytea by hand.
  3. Confirm bytea_output/client settings match the format you send.

Related & next steps

Reference: PostgreSQL 18 Section 8.4 “Binary Data Types”.

Was this helpful?