FluentDBFluentDB
PostgreSQL errors

PostgreSQL: value too long for type character varying(n)

ERRORSQLSTATE 22001string_data_right_truncation
ERROR: value too long for type character varying(<n>)

A string exceeded the length limit of a varchar(n) column. Widen the column, switch to text, or truncate the input to fit.

What this error means

A varchar(n) column enforces a maximum length. When you insert a longer string, Postgres rejects it rather than silently truncating.

Multi-byte characters and concatenation can push a value over the limit unexpectedly.

How to fix it

01

The column is too narrow

Widen it, or switch to text which has no fixed limit.

sql
ALTER TABLE t ALTER COLUMN name TYPE varchar(500);
-- or remove the limit entirely:
ALTER TABLE t ALTER COLUMN name TYPE text;
02

The input is genuinely too long

Validate and truncate the input in the application before insert.

How to avoid it next time

  • Prefer text unless a hard length limit is a real requirement.
  • Validate string lengths at the application boundary.

Frequently asked questions

Is text slower than varchar in Postgres?

No. text and varchar have the same performance in Postgres. varchar(n) only adds a length check. Use text unless you truly need the limit.

Why does a short-looking string fail?

Multi-byte or combining characters can make the stored length longer than the visible length. Count characters, not screen width.

Stop struggling with SQL

Use FluentDB, the AI database client for macOS.