The input string cannot be converted to the requested data type.
ERROR 22018 invalid_character_value_for_cast appears when PostgreSQL cannot convert a text literal to the target data type. Clean or re-format the input, then retry the CAST or assignment.
ERROR 22018 invalid_character_value_for_cast
The server throws ERROR 22018 when it attempts to cast a character string into a numeric, date, UUID, or other typed column and finds illegal characters.
The check happens during INSERT, UPDATE, explicit CAST(), or implicit conversions in operators, functions, and constraint checks.
PostgreSQL halts the statement once the first invalid character is detected.
The statement fails and any transactional work is rolled back. Applications that swallow the exception can lose data or mask bugs. Fixing the root cause ensures data integrity and prevents repeated runtime failures.
The server reports the exact column and value when possible.
If the error bubbles out of a function, additional context lines show the call stack. Use psql’s \errverbose or modern IDEs like Galaxy to reveal the source location quickly.
.
Yes. Multi-byte encodings can hide non-printable characters that break casts. Use encode() to inspect raw bytes.
Use COPY ... FROM ... WITH (FORMAT csv) and the 'log_errors' extension or import into a staging table, then clean.
Galaxy’s inline result previews highlight failed rows instantly, and its AI copilot suggests safe CAST patterns based on your schema.
Native TRY_CAST arrives in PG16. Before that, create a wrapper function that returns NULL on failure or use exception blocks.