How to Fix Postgres Error 22P02 Invalid Text Representation

Published
Length
3 min read

Luis Felipe Dias

All posts

The postgres error 22P02 invalid text representation happens when the database cannot convert text into a target data type. You fix it by cleaning input strings or validating types before running the query. Database engines enforce strict rules when they store and compare data. When an app sends an empty string to an integer column, the query fails right away. This stop protects your tables from corrupt records and broken math. Root causes of postgres error 22P02 invalid text representation

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

The postgres error 22P02 invalid text representation happens when the database cannot convert text into a target data type. You fix it by cleaning input strings or validating types before running the query.

Database engines enforce strict rules when they store and compare data. When an app sends an empty string to an integer column, the query fails right away. This stop protects your tables from corrupt records and broken math.

Root causes of postgres error 22P02 invalid text representation

In the official system specs, SQLSTATE 22P02 maps to the ERRCODE_INVALID_TEXT_REPRESENTATION macro. The engine raises this code when a literal string does not match the target type format. The parser reads the text, finds an illegal character, and stops the transaction.

The most common trigger is passing non-numeric letters into an integer or numeric field. Empty strings also cause this failure because an empty string is not a valid number. You must send a true null value instead of blank text when a number is missing.

UUID columns are another frequent source of this failure in production apps. A valid UUID needs thirty-two hexadecimal digits arranged in a specific hyphenated pattern. If an app passes a short ID or a plain word into a UUID filter, the parser halts.

JSON and array columns also trigger 22P02 when input text has broken syntax. Missing quotes, extra commas, or wrong brackets make the text unreadable to the parser. Boolean columns fail too if the input text is not a recognized true or false word.

How to diagnose and fix 22P02 in production

To find the bad input, check your database error logs first. The log message shows the exact string value that failed the type check. Match that string against the column types in your target table.

When you inspect production data, do not use a superuser account. Set up group roles to manage permissions for your team members. Grant the select privilege only on the specific columns you need to check.

End-to-End Encryption + Text-Based Session Recording: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Remember that the special role name PUBLIC grants rights to every role on the system. By default, PUBLIC gets connect and temporary rights on databases, plus execute rights on functions. Lock down your schemas so debug users cannot alter tables while hunting for bad strings.

Once you find the bad input, fix the problem in your app code first. Validate user forms so blank strings turn into null values before reaching the database. Check that UUID strings match the required format before you run a query.

If bad text already lives in a staging text column, clean it before casting. Use safe conditional expressions to check the string format before converting it to a number. If you use data masking on emails or phone numbers, keep the masked format valid for that column type.

Preventing type cast failures in data pipelines

Bulk data loads often fail with 22P02 when reading messy CSV files. A single row with a stray letter in a price column stops the whole import. Load raw files into a staging table with text columns first, then validate each row.

In version 15 and later, default settings stop every user from creating objects in the public schema. On older databases, an admin must revoke create rights on the public schema from PUBLIC. Use dedicated staging schemas with strict role access for your data cleanup jobs.

Keep your app models and database schemas in sync during every release. When you change a column from text to integer, audit all existing rows first. Clear out blank spaces and hidden characters so the schema change runs clean.

Run guardrails and masking on the Postgres connection

Role grants and firewall rules are needed setup. Setup alone cannot check single SQL statements or mask sensitive rows in a response. By design, hoop.dev sits inline in the Postgres protocol beside Postgres, RDS, or Aurora as a sidecar for your Postgres. On every production query from an engineer or AI agent, hoop.dev decides whether to pass, mask, block, or hold it for approval.

First, hoop.dev guardrails refuse disallowed drops or deletes before they run, and risky statements can pause for human review. Next, hoop.dev data masking rewrites sensitive fields in returned rows in memory. Furthermore, hoop.dev holds SOC 2 Type II and logs every statement and verdict per resource. Check out the project on GitHub.

Frequently asked questions

Why does an empty string cause error 22P02 on integer columns?

An empty string is still a text value, not a number or a null marker. The database parser cannot turn zero characters into a valid digit, so it rejects the query. Convert empty strings to null in your app before sending the data.

Can data masking trigger invalid text representation errors?

Yes, if a masking rule replaces numbers with letters in a numeric column. Masking rewrites matched fields like card numbers or national IDs in query results. Always make sure the rewritten text matches the data type expected by your app.

How do I find which column caused the 22P02 failure?

Read the detailed error output in your server log right after the query fails. The log prints the exact rejected text value right next to the 22P02 code. Compare that printed value against the parameter list in your query to spot the broken field.

The query runs. The emails stay hidden.