SQLSTATE 22001 ERROR Class 22: Data Exception

string_data_right_truncation value too long for type character varying(N) — 22001

PostgreSQL error "value too long for type character varying(N)" (SQLSTATE 22001): what it means, common causes, and how to fix it.

PG 12, 13, 14, 15, 16, 17, 18 Official docs
Last reviewed May 2025 Grounded in source

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

  1. Widen the column: ALTER TABLE t ALTER COLUMN name TYPE varchar(255); (or use text).
  2. Validate/limit input length in the application.
  3. Deliberately truncate when acceptable: left(value, 20).

Related & next steps

Reference: PostgreSQL 18 Section 8.3 “Character Types”.

Was this helpful?