sqlstreak

SQL error

value too long for type character varying(3)

Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)

Short answer

The value has more characters than the column allows. Widen the column (ALTER TABLE … ALTER COLUMN code TYPE VARCHAR(10)), shorten the value, or use TEXT, which has no length limit and costs nothing extra in PostgreSQL.

The exact messages

  • PostgreSQLERROR: value too long for type character varying(3)
  • SQLiteNo error, but a silent surprise: see below.

How to read this message

This is what PostgreSQL 18.6 prints for the broken query below, the way psql shows it.

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

Why it happens

A limit chosen too small

Codes, names and phone numbers are often longer than expected (country prefixes, accents, spaces).

Data moved from SQLite or a spreadsheet

SQLite never checked the length, so over-long values were already there.

Reproduce it, then fix it

Both queries run on a real database in your browser. Switch dialects to compare.

Broken
CREATE TABLE codes (code VARCHAR(3));
INSERT INTO codes VALUES ('ABCD');
SELECT * FROM codes;
Fixed
CREATE TABLE codes (code TEXT);
INSERT INTO codes VALUES ('ABCD');
SELECT * FROM codes;

TEXT takes any length. If the limit is a real rule, keep VARCHAR(n) and check or trim values before inserting.

The fix returns, PostgreSQL: 1 row

code
ABCD

The fix returns, SQLite: 1 row

code
ABCD

Experiment with it: PostgreSQL playground·SQLite playground

Another fix

Or cut the value to fit, if losing the end is acceptable.

CREATE TABLE codes (code VARCHAR(3));
INSERT INTO codes VALUES (SUBSTR('ABCD', 1, 3));
SELECT * FROM codes;

Returns, PostgreSQL: 1 row

code
ABC

Returns, SQLite: 1 row

code
ABC

What SQLite does instead

SQLite accepts the declaration but never enforces the length: 'ABCD' goes into a VARCHAR(3) column whole. Data that loads fine in SQLite can fail in PostgreSQL.

Official documentation

Stop hitting this error

Practise the concept behind it in a five-minute lesson, or read the full guide: SQL CREATE TABLE, constraints and views.