Symptoms
A string value is too long for the target character column.
- The error is written to the server log and returned to the client carrying
SQLSTATE 22001. - Any driver (libpq, JDBC, psycopg, npgsql, pgx) surfaces this code in its error object so you can branch on it programmatically.
- PL/pgSQL can trap it by name:
EXCEPTION WHEN string_data_right_truncation THEN.
Environment
Severity: ERROR | PostgreSQL versions: 12, 13, 14, 15, 16, 17
Reproduce with the exact statement and read the full message in the server log (raise log_min_messages / set log_min_error_statement for more context).
Root Cause
The value exceeds the declared length of a varchar(n) or char(n) column or cast.
Common causes:
- Inserting or updating text longer than the column width.
- An explicit cast to a too-small
varchar(n). - Multibyte characters making the string longer than expected.
Diagnostic Queries
Recovery
Steps to resolve 22001:
- Widen the column:
ALTER TABLE t ALTER COLUMN c TYPE varchar(NNN);, or usetextfor no limit. - Validate or trim input length in the application before writing.
- If truncation is acceptable, apply it explicitly with
left(value, n).
Reference: PostgreSQL error codes — Class 22 (Data Exception).
Thanks — noted. This helps keep the database accurate.