22001
PostgreSQLERRORCommonData ExceptionHIGH confidence

value too long for type character varying

What this means

An attempt was made to store a string value into a VARCHAR(n) or CHAR(n) column where the string's length in characters exceeds the declared limit n. Postgres enforces this limit in the executor before writing to the heap.

Why it happens
  1. 1Input data is longer than the column's declared character limit
  2. 2Column length was reduced with ALTER TABLE ALTER COLUMN TYPE without checking existing data
  3. 3Multibyte UTF-8 characters being counted incorrectly at the application layer (byte length vs character length)
  4. 4Truncation that was expected to happen silently (as in some other databases) does not occur in Postgres by default
How to reproduce

An INSERT attempts to store a 12-character string in a VARCHAR(10) column.

trigger — this will error
trigger — this will error
CREATE TABLE products (
  id   SERIAL PRIMARY KEY,
  code VARCHAR(10) NOT NULL
);

INSERT INTO products (code) VALUES ('TOOLONGVALUE'); -- 12 chars, limit is 10

expected output

ERROR:  value too long for type character varying(10)

Fix 1

Increase the column length limit

WHEN When the data is legitimately longer than the original limit and the schema should be relaxed.

Increase the column length limit
ALTER TABLE products ALTER COLUMN code TYPE VARCHAR(50);

Why this works

Increasing a VARCHAR(n) limit in Postgres does not rewrite the table; it only updates the pg_attribute entry when the new limit is larger than the old one. The executor checks the new limit on subsequent writes. This is an O(1) metadata-only change.

Fix 2

Change to TEXT (no length limit)

WHEN When there is no meaningful business constraint on length and VARCHAR was used out of habit.

Change to TEXT (no length limit)
ALTER TABLE products ALTER COLUMN code TYPE TEXT;

Why this works

TEXT and VARCHAR without a limit are stored identically in Postgres (both use the varlena storage format). There is no performance difference. Switching to TEXT removes the length check from the executor entirely.

Fix 3

Truncate the input value in the query

WHEN When the business rule requires enforcing the limit by truncating rather than rejecting.

Truncate the input value in the query
INSERT INTO products (code)
VALUES (LEFT('TOOLONGVALUE', 10)); -- truncates to 'TOOLONGVAL'

Why this works

LEFT(str, n) returns the first n characters. The truncation happens in executor expression evaluation before the tuple is formed, so the stored value satisfies the column limit.

What not to do

Cast the value to VARCHAR without a limit to bypass the error

The cast succeeds in the expression but the column constraint still applies at insert time; use ALTER TABLE to fix the schema.

Sources
Official documentation ↗

src/backend/utils/adt/varchar.c — varchar()

Character Types

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All PostgreSQL errors