PostgreSQL 17.12Lab-verified · 2026-12-01

PostgreSQL error 22001: value too long for type character varying

The value has more characters than the varchar(n) column allows.

PostgreSQL 17.12
22001: value too long for type character varying(13)

Jump to a cause

  1. 01The value is longer than the column (column too small)
  2. 02Multibyte text: the limit is in characters, and the text really is longer
  3. 03INSERT ... SELECT from a source column that is wider
  4. 04COPY loads a CSV, and one line has a value that is too long
  5. 05UPDATE appends text to the existing value

Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-12-01 · container image postgres:17

22001 means a string is longer than the column's declared length. The message shows the type, such as character varying(13), but not the column name or the value, so you have to find which column has that length (COPY is the exception: its CONTEXT names the line, column, and value).

In PostgreSQL the n in varchar(n) is a number of characters, not bytes. The whole statement is rejected; no row is inserted.

01The value is longer than the column (column too small)

phone varchar(13) was sized for numbers like 010-1234-5678. An international number +82-10-1234-5678 is 16 characters, so the insert fails with character varying(13).

Setup
Setuprun as lab_a
CREATE TABLE members (id int PRIMARY KEY, phone varchar(13));
OUTPUT
CREATE TABLE
Reproducerun as lab_a
INSERT INTO members VALUES (1, '+82-10-1234-5678');
✕ ERROR
ERROR:  22001: value too long for type character varying(13)
Checkrun as lab_a
SELECT length('+82-10-1234-5678') AS len;
OUTPUT
 len
-----
  16
(1 row)
Fixrun as lab_a
ALTER TABLE members ALTER COLUMN phone TYPE varchar(20);
OUTPUT
ALTER TABLE

length() shows 16. When the data is legitimately longer, widen the column: ALTER TABLE members ALTER COLUMN phone TYPE varchar(20);. After that the same INSERT succeeds (INSERT 0 1).

After the fixrun as lab_a
INSERT INTO members VALUES (1, '+82-10-1234-5678');
✓ SUCCESS
INSERT 0 1

02Multibyte text: the limit is in characters, and the text really is longer

name varchar(10) rejects a 15-character Korean product name. The check shows 15 characters and 39 bytes. The limit is compared with the 15 characters, not the bytes; a 10-character Korean value fits in varchar(10).

Setup
Setuprun as lab_a
CREATE TABLE products (id int PRIMARY KEY, name varchar(10));
OUTPUT
CREATE TABLE
Reproducerun as lab_a
INSERT INTO products VALUES (1, '무선 블루투스 이어폰 화이트');
✕ ERROR
ERROR:  22001: value too long for type character varying(10)
Checkrun as lab_a
SELECT length('무선 블루투스 이어폰 화이트') AS chars, octet_length('무선 블루투스 이어폰 화이트') AS bytes;
OUTPUT
 chars | bytes
-------+-------
    15 |    39
(1 row)

Here the value was cut to the column size with left(..., 10) and inserted. Cutting loses data (the name becomes 무선 블루투스 이어), so widen the column instead if the full value matters.

After the fixrun as lab_a
INSERT INTO products VALUES (1, left('무선 블루투스 이어폰 화이트', 10));
SELECT id, name FROM products;
✓ SUCCESS
INSERT 0 1
 id |        name
----+--------------------
  1 | 무선 블루투스 이어
(1 row)

03INSERT ... SELECT from a source column that is wider

The staging table uses text, the target uses varchar(5). One source row, C0002-OLD, is 9 characters, and the whole INSERT ... SELECT fails, so even the short C001 is not copied. The message does not say which row.

Setup
Setuprun as lab_a
CREATE TABLE src_customers (code text, name text);
INSERT INTO src_customers VALUES ('C001', 'Kim'), ('C0002-OLD', 'Lee');
CREATE TABLE customers (code varchar(5) PRIMARY KEY, name text);
OUTPUT
CREATE TABLE
INSERT 0 2
CREATE TABLE
Reproducerun as lab_a
INSERT INTO customers SELECT code, name FROM src_customers;
✕ ERROR
ERROR:  22001: value too long for type character varying(5)
Checkrun as lab_a
SELECT code, length(code) FROM src_customers WHERE length(code) > 5;
OUTPUT
   code    | length
-----------+--------
 C0002-OLD |      9
(1 row)

Find the offending rows with WHERE length(code) > 5. Here the suffix was removed with split_part(code, '-', 1) and both rows were copied (INSERT 0 2). Choose the conversion that is right for your data, or widen the target column.

After the fixrun as lab_a
INSERT INTO customers SELECT split_part(code, '-', 1), name FROM src_customers;
SELECT code, name FROM customers ORDER BY code;
✓ SUCCESS
INSERT 0 2
 code  | name
-------+------
 C0002 | Lee
 C001  | Kim
(2 rows)

04COPY loads a CSV, and one line has a value that is too long

Three CSV lines were loaded with COPY members FROM STDIN, and the city on line 3, Gyeongsangbuk-do (16 characters), did not fit in varchar(10). COPY's CONTEXT names the line (line 3), the column (column city), and the value.

COPY is one statement, so a single bad line cancels the whole load. Even the two good lines were not inserted: loaded is 0.

Setup
Setuprun as lab_a
CREATE TABLE members (id int PRIMARY KEY, name varchar(10), city varchar(10));
OUTPUT
CREATE TABLE
Reproducerun as lab_a
COPY members FROM STDIN WITH (FORMAT csv);
1,Kim,Seoul
2,Lee,Busan
3,Park,Gyeongsangbuk-do
\.
✕ ERROR
ERROR:  22001: value too long for type character varying(10)
CONTEXT:  COPY members, line 3, column city: "Gyeongsangbuk-do"
Checkrun as lab_a
SELECT count(*) AS loaded FROM members;
SELECT length('Gyeongsangbuk-do') AS len;
OUTPUT
 loaded
--------
      0
(1 row)

 len
-----
  16
(1 row)
Fixrun as lab_a
ALTER TABLE members ALTER COLUMN city TYPE varchar(30);
OUTPUT
ALTER TABLE

After widening city to varchar(30), loading the same CSV again gave COPY 3, all three lines. If the value should be shorter, fix the source file instead.

After the fixrun as lab_a
COPY members FROM STDIN WITH (FORMAT csv);
1,Kim,Seoul
2,Lee,Busan
3,Park,Gyeongsangbuk-do
\.
✓ SUCCESS
COPY 3

05UPDATE appends text to the existing value

The UPDATE appended ' (sold out)' to product names. Desk Lamp becomes exactly 20 characters and fits, but Wireless Keyboard becomes 28 and exceeds varchar(20). This message does not say which row.

If any row is too long, the whole UPDATE fails, so product 1, which would fit, is not changed either.

Setup
Setuprun as lab_a
CREATE TABLE products (id int PRIMARY KEY, name varchar(20));
INSERT INTO products VALUES (1, 'Desk Lamp'), (2, 'Wireless Keyboard');
OUTPUT
CREATE TABLE
INSERT 0 2
Reproducerun as lab_a
UPDATE products SET name = name || ' (sold out)' WHERE id IN (1, 2);
✕ ERROR
ERROR:  22001: value too long for type character varying(20)
Check the causerun as lab_a
SELECT id, name, length(name || ' (sold out)') AS new_len FROM products ORDER BY id;
OUTPUT
 id |       name        | new_len
----+-------------------+---------
  1 | Desk Lamp         |      20
  2 | Wireless Keyboard |      28
(2 rows)

new_len is 20 for row 1 and 28 for row 2. Row 2 exceeds varchar(20) and makes the statement fail.

Fixrun as lab_a
ALTER TABLE products ADD COLUMN sold_out boolean NOT NULL DEFAULT false;
OUTPUT
ALTER TABLE

Keeping the status in its own sold_out column instead of the name let both rows update. If it really must go into the name, widen the column.

After the fixrun as lab_a
UPDATE products SET sold_out = true WHERE id IN (1, 2);
SELECT id, name, sold_out FROM products ORDER BY id;
✓ SUCCESS
UPDATE 2
 id |       name        | sold_out
----+-------------------+----------
  1 | Desk Lamp         | t
  2 | Wireless Keyboard | t
(2 rows)

FAQ

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

Find the column with that length (the message shows only the type), check the value with length(), then either widen the column with ALTER COLUMN ... TYPE varchar(n) or shorten the value.

Is varchar(n) in PostgreSQL measured in bytes or characters?

Characters. In the test, a Korean string of 15 characters and 39 bytes was rejected by varchar(10), and its first 10 characters fit.

How do I find which row is too long in INSERT ... SELECT?

Query the source with WHERE length(col) > n. The error does not name the row, and the whole statement is rolled back.

COPY fails with value too long. How do I find the line?

COPY prints it in CONTEXT: line number, column, and value. In the run above it was COPY members, line 3, column city: "Gyeongsangbuk-do". One bad line cancels the whole COPY, so none of the other lines are loaded.

How we verify

Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.

lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12