Diagnostic Queries
Symptoms
A string is longer than the declared length of a varchar(n)/char(n) column. PostgreSQL refuses to truncate silently and raises SQLSTATE 22001 (string_data_right_truncation).
- The message includes the maximum length
(n). - Common after a UI accepts longer input than the column allows.
- Affects INSERT, UPDATE, and casts to a length-limited type.
What the server log shows
ERROR: value too long for type character varying(20)
Why PostgreSQL raises this — what the manual says
Section 8.3 Character Types:
“However, if one explicitly casts a value to character varying(n) or character(n), then an over-length value will be truncated to n characters without raising an error.”
A length-qualified character type enforces its maximum on assignment. A value exceeding n (with non-space overflow) cannot be stored without data loss, so PostgreSQL aborts with 22001 rather than truncating.
Common causes
- Input longer than the column’s declared length.
- An under-sized column relative to real data.
- Concatenation producing a longer string than expected.
How to fix it
- Widen the column:
ALTER TABLE t ALTER COLUMN name TYPE varchar(255);(or usetext). - Validate/limit input length in the application.
- Deliberately truncate when acceptable:
left(value, 20).
Related & next steps
Reference: PostgreSQL 18 Section 8.3 “Character Types”.
Thanks — noted. This helps keep the database accurate.