Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

PostgreSQL Error 22P03: Invalid Binary Representation Fix

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Error 22P03 in PostgreSQL means the server received a binary-format parameter or result value that does not match the expected binary layout for that data type. The fix is almost always on the client side: stop sending binary-format parameters, or make sure the driver and the server agree on the type and format. In practice you resolve it by setting the connection or statement to text format, updating the driver, or correcting the value you are binding. The steps below walk through identifying the source, applying the safest fix first, and verifying that the error is gone.

What You'll Need

  • Access to the application or script that produces the error, including its database driver and connection settings.
  • The full PostgreSQL error message, which usually names the parameter or column involved.
  • Permission to change connection parameters or driver settings in the application.
  • Access to the PostgreSQL server logs if you need to confirm which statement failed.
  • A test database or a safe way to reproduce the failing query without affecting production data.

Step-by-Step

  1. Read the full error text. PostgreSQL reports 22P03 as invalid binary representation and normally adds a detail line such as the type name and the byte length it received. Write down the type (for example int4, timestamp, numeric or bytea) and the statement that triggered it. This tells you whether the problem is a parameter you sent or a result the server tried to return.
  2. Identify the client and driver. Note the language, the driver name and its version (for example a JDBC driver, psycopg, Npgsql, node-postgres or a Go driver). Binary representation errors are strongly driver-specific: some drivers default to binary parameter format for performance, others use text. Check the driver version in your dependency file or package manager.
  3. Reproduce the failure in isolation. Run the same query from a simple client such as psql with the same values. If psql succeeds, the data and SQL are fine and the problem is in the driver's binary encoding. If psql also fails, the value itself is malformed and you should fix the data first.
  4. Switch the connection or statement to text format. This is the safest and most common fix. In most drivers you can force text format for parameters and results, either as a connection option or per statement. The exact setting name differs by driver and version, so check your driver's documentation for the option that controls binary transfer or parameter format. Labels and option names vary between driver versions.
  5. Update the driver and the server client libraries. A mismatch between a driver's binary encoding and the server's expected layout for a type is a known cause, especially after a PostgreSQL major-version upgrade. Upgrade the driver to a version that officially supports your server version, and restart the application.
  6. Check the column and parameter types. If you bind a value as one type but the column is another, a binary encoding for the wrong type will be rejected. Cast explicitly in SQL, for example WHERE id = $1::int, or bind the parameter with the correct type in your driver's API. This matters most for numeric, timestamp, interval, UUID and array types.
  7. Look for custom types or extensions. If the failing type is a domain, enum, composite or extension type, the driver may not know its binary layout. Cast the value to a base type in the query, or send that parameter in text format. Custom types are the most common source of 22P03 in otherwise healthy applications.
  8. Restart and retest. After changing driver settings, restart the application so pooled connections are recreated with the new options. Re-run the failing operation and confirm the error is gone. Check the server log once more to be sure no other statement is still failing.

Common Mistakes

  • Assuming the database is corrupt. 22P03 is a format mismatch between client and server, not damaged storage.
  • Changing server settings first. The server is doing what it was told; fix the client's encoding or type binding.
  • Ignoring the detail line. The type name in the error usually points straight at the offending parameter.
  • Forgetting connection pooling. Old pooled connections keep the previous format settings until they are recycled.
  • Binding a string to a numeric or timestamp parameter and expecting the driver to convert it correctly in binary mode.

Tips

  • Prefer text format for parameters unless you have measured a real performance need for binary transfer.
  • Keep driver versions aligned with the PostgreSQL major version you run.
  • Log the SQL and parameter types, not the values, when diagnosing format errors; this avoids leaking sensitive data.
  • Test driver upgrades against a staging copy before rolling them out.
  • When using an ORM, check how it binds custom types; many allow an explicit type mapping.

Troubleshooting

If the error persists after switching to text format, confirm the setting actually applied by checking the driver's debug or query log. If only one query fails, isolate the parameter types and cast them explicitly in SQL. If the failure started after a server upgrade, roll the driver forward rather than backward, and review the driver's release notes for binary-format changes. If the failing type is an extension type, cast to a base type or send it as text. If you cannot change the driver, move the affected logic to a stored function that accepts text parameters and casts internally. When all else fails, capture the exact statement, parameter types and driver version, and search the driver's issue tracker; 22P03 reports are usually specific and reproducible.

Frequently Asked Questions

What does PostgreSQL error 22P03 mean?

It means a value arrived in binary format but did not match the binary layout PostgreSQL expects for that data type.

Is my data corrupted?

No. The error is about how the value was transmitted, not about stored data being damaged.

Can I fix it without changing code?

Sometimes. Forcing text format through a connection option may be enough, but custom types often need a cast or a driver update.

Why did it appear after an upgrade?

Driver and server versions can disagree on binary encoding for some types. Updating the driver to a version that supports your server resolves most cases.

Does this affect all queries?

Usually only queries that use binary parameters or return binary results for the affected type. Text-format queries keep working.

Should I disable binary transfer permanently?

It is a safe default for correctness. Only enable binary transfer where you have verified driver support and measured a benefit.