Skip to content
PostgreSQL

PostgreSQL Error 22001

value too long for type character varying

Applies to: PostgreSQL 13 to 18 (examples run on PostgreSQL 18.6 in PGlite)
Last technically reviewed 24 October 2026 on PostgreSQL 18.6 (PGlite 0.5.11)
The full message
ERROR:  value too long for type character varying(3)
Other forms of this message
Fixed-length character(n) column
ERROR:  value too long for type character(2)
During COPY (CONTEXT names the line and column)
ERROR:  value too long for type character varying(3)
CONTEXT:  COPY emp, line 1, column code: "ABCD"
Inside a PL/pgSQL function (CONTEXT names the function and line)
ERROR:  value too long for type character varying(3)
CONTEXT:  PL/pgSQL function f(character varying) line 1 at assignment
SQLSTATE
22001
Condition name
string_data_right_truncation
Product
PostgreSQL
Versions
PostgreSQL 13 to 18 (examples run on PostgreSQL 18.6 in PGlite)
Last reviewed
24 October 2026

What it means

SQLSTATE 22001 means a string is longer than the declared length of a varchar(n) or char(n) column or variable, so PostgreSQL rejected it instead of cutting it short.

A column declared as varchar(n) (character varying) or char(n) (character) can hold at most n characters; the limit counts characters, not bytes. Storing a longer string with INSERT, UPDATE or COPY, or assigning it to such a variable in PL/pgSQL, raises 22001 (string_data_right_truncation). The message gives only the type and length, not the column name, so with several columns of the same length you have to compare the values yourself; in COPY, the CONTEXT line names the line and column.

Two exceptions come from the SQL standard and can be surprising. If the extra characters are all spaces, the string is silently truncated to the maximum length. And an explicit cast, 'ABCD'::varchar(3), truncates to n characters without an error. Changing a column to a shorter length with ALTER TABLE fails with the same error when existing rows are too long.

Common causes

  • The column was sized for typical data (varchar(50) for names, varchar(3) for codes) and real data is longer.
  • An import or INSERT ... SELECT copies from a staging table of text columns without checking lengths.
  • An UPDATE appends to a value (code || 'X'), pushing it past the limit.
  • A char(2) country column receives a three-letter code, or a code column receives a full name.
  • ALTER TABLE ... ALTER COLUMN ... TYPE varchar(n) shortens a column that already holds longer values.

Diagnose it

Read-only These queries only read the system catalogs (pg_catalog, information_schema) and statistics views such as pg_stat_activity and pg_locks. They do not change data, roles or settings, so they are safe to run first.

Which columns have a length limit?

SELECT column_name, data_type, character_maximum_length
FROM information_schema.columns
WHERE table_name = 'users'
  AND character_maximum_length IS NOT NULL
ORDER BY ordinal_position;
What to look for

Every varchar(n) and char(n) column with its limit. Columns whose limit matches the n in the message are the candidates.

Find the source values that are too long

SELECT code, char_length(code) AS chars
FROM staging_users
WHERE char_length(code) > 3
ORDER BY chars DESC;
What to look for

The rows that would fail, longest first. Decide whether the column is too small or the data is wrong.

Before shortening a column, check the longest stored value

SELECT max(char_length(note)) AS longest_note
FROM users;
What to look for

The longest value currently stored. ALTER COLUMN ... TYPE varchar(n) fails if this is greater than n.

How to fix it

Each fix is labelled with what it changes. Try read-only checks and the least invasive fix first, and test changes outside production.

Widen the column, or use text

Changes schema

If the data is right and the limit is too small, increase it. In PostgreSQL, text and varchar without a length perform the same as varchar(n); a length limit is only useful as a business rule. A CHECK constraint on char_length expresses that rule and is easier to change later.

Caution: ALTER COLUMN ... TYPE takes an ACCESS EXCLUSIVE lock, which blocks reads and writes while it runs, and some type changes rewrite the table (the ALTER TABLE notes explain when a rewrite is avoided). Test it on a copy of a large table first, and check views that depend on the column.

ALTER TABLE users
    ALTER COLUMN code TYPE varchar(10);

Reject or fix the over-long data before loading

Changes data

Load only rows that fit, and keep the others for review using the read-only check above.

INSERT INTO users (code)
SELECT code
FROM staging_users
WHERE char_length(code) <= 3;

Truncate deliberately, only when cutting is acceptable

Changes data

If shortening is really what you want (a display label, a summary), say so explicitly with LEFT.

Caution: Truncation loses data silently from then on. Never use it for codes, identifiers or anything that must match another system exactly.

INSERT INTO users (note)
SELECT left(note, 10)
FROM staging_notes;

Examples

A four-character code into varchar(3)

CREATE TABLE users (id int, code varchar(3));

INSERT INTO users (code) VALUES ('ABCD');
Result
ERROR:  value too long for type character varying(3)

Reproduced on PGlite 0.5.11 (PostgreSQL 18.6 compiled to WebAssembly), 24 October 2026.

A three-letter country code into char(2)

CREATE TABLE users (id int, country char(2));

INSERT INTO users (country) VALUES ('GBR');
Result
ERROR:  value too long for type character(2)

Reproduced on PGlite 0.5.11 (PostgreSQL 18.6 compiled to WebAssembly), 24 October 2026.

Loading from a staging table

The whole INSERT fails, including the row that fits, and the message does not say which row was too long. The read-only check above finds it.

CREATE TABLE users (id int, code varchar(3));
CREATE TABLE staging_users (code text);
INSERT INTO staging_users VALUES ('AB'), ('ABCDE');

INSERT INTO users (code) SELECT code FROM staging_users;
Result
ERROR:  value too long for type character varying(3)

Reproduced on PGlite 0.5.11 (PostgreSQL 18.6 compiled to WebAssembly), 24 October 2026.

Shortening a column that holds longer values

CREATE TABLE users (id int, code varchar(3));
INSERT INTO users (code) VALUES ('ABC');

ALTER TABLE users ALTER COLUMN code TYPE varchar(2);
Result
ERROR:  value too long for type character varying(2)

Reproduced on PGlite 0.5.11 (PostgreSQL 18.6 compiled to WebAssembly), 24 October 2026.

How to prevent it

Give columns a length limit only when there is a real rule behind it (an ISO code, a fixed format); otherwise use text. Validate lengths in the application with the same limits as the database, counting characters rather than bytes.

For imports, load into text staging columns, check char_length against the target limits, and report rows that do not fit instead of letting one row fail the whole load.

Common questions

How do I fix "value too long for type character varying" in PostgreSQL?

Find the column with that length (information_schema.columns) and the value that is too long (char_length in the source). Then either widen the column, or fix or reject the data. Truncate with left() only when cutting the value is acceptable.

Which column caused it? The message does not say.

PostgreSQL reports the type, not the column. Compare the n in the message with the lengths of the table's columns, and check the inserted values against them. Most drivers do not add the column either; in COPY, the CONTEXT line names it.

Why did a cast to varchar(3) not raise the error?

An explicit cast such as value::varchar(3) truncates to three characters without an error, as the SQL standard requires. The error is raised when storing into a column or variable, unless only spaces are cut off.

Learn more on CodeWithSQL

Official documentation

Last technically reviewed 24 October 2026; examples and diagnostics run on PostgreSQL 18.6 (PGlite 0.5.11). SQL-level examples were run in PGlite (PostgreSQL compiled to WebAssembly); examples that need a running server are shown as documented in the PostgreSQL documentation.

Still stuck on a PostgreSQL error?

Browse the full PostgreSQL error library, or get hands-on help from an experienced SQL professional.