PostgreSQL error: value too long for type character varying
ERROR: value too long for type character varying(50)
A string exceeded the declared length limit of its column, and PostgreSQL rejected the write rather than silently truncating it. Annoyingly, the message names the type but not the column — finding which column is step one.
What this error means
Two PostgreSQL-specific facts frame the fix:
- There is no performance benefit to
varchar(n)overtextin PostgreSQL — they are stored identically. The limit is purely a constraint, so it should exist only where the business actually has one. - Widening a varchar does not rewrite the table — increasing the limit is a quick catalog-only change.
Common causes
- Real data outgrew an arbitrary limit — the cargo-cult
varchar(255)meeting a long URL, user agent or address. - Concatenation or denormalized fields growing past the original estimate.
- Imports with wrong column mapping putting long values into short fields.
How to diagnose it
-- Quali colonne varchar(50) ha la tabella sospetta:
SELECT column_name, character_maximum_length
FROM information_schema.columns
WHERE table_name = 'users' AND character_maximum_length IS NOT NULL;
Then compare against the payload that failed. If the statement is logged, the values are right there.
How to fix it
- Widen it (fast, catalog-only):
ALTER TABLE users ALTER COLUMN name TYPE varchar(200); - Or drop the arbitrary limit entirely —
text, with a CHECK constraint if some bound genuinely matters:
A CHECK is easier to adjust later than a type.ALTER TABLE users ALTER COLUMN name TYPE text; ALTER TABLE users ADD CONSTRAINT name_reasonable CHECK (length(name) <= 1000); - Truncate client-side only if truncation is genuinely acceptable (
left(value, 50)) — that is a product decision, not a database default, which is exactly why PostgreSQL refuses to make it for you.
