How to fix ERROR: value too long for type character varying(n) in PostgreSQL
Postgres rejects an INSERT/UPDATE when a string exceeds a VARCHAR(n) column limit. Fix by widening the column with ALTER TABLE or truncating input.
Quick fix
Fixes ERROR: value too long for type character varying(n)
\d table_name- Identify which column caused the error
- Increase the column length (recommended)
- Truncate the value explicitly (if appropriate)
- Use TEXT type for unlimited length
\d table_nameERROR: value too long for type character varying(n)
What this error means
In PostgreSQL, VARCHAR(n) and CHARACTER VARYING(n) columns have a strict length constraint of n characters (not bytes). When you try to insert or update a row with a string that exceeds this limit, PostgreSQL raises this error to maintain data integrity. This is different from some other databases that might silently truncate values. PostgreSQL enforces these constraints strictly unless you explicitly cast the value using VARCHAR(n) syntax, which truncates without error.
Typical symptoms
- INSERT or UPDATE statements fail with an explicit error message
- Application receives database constraint violation errors
- Error message may not identify which specific column is too long when multiple VARCHAR columns exist
- Data import or migration tools fail during batch operations
Common causes
- User input or data exceeds the column's defined VARCHAR(n) length limit
- Application code sends strings longer than the database schema allows
- Data migration from a system with no length constraints to one with strict limits
- Character encoding differences (e.g., UTF-8 multibyte characters counted differently)
- Database schema was created with insufficient VARCHAR length for actual data needs
How to fix "ERROR: value too long for type character varying(n)"
The error message from PostgreSQL may not always clearly indicate which column rejected the value. Check your application logs to see which INSERT or UPDATE operation failed, then identify the column being populated. You can also examine your table definition:
\d table_nameThis shows all columns and their VARCHAR length constraints.
If the data legitimately needs to be longer, alter the table to increase the VARCHAR limit:
ALTER TABLE users ALTER COLUMN email TYPE varchar(255);This approach preserves all data and accommodates longer values. There is no performance penalty in PostgreSQL for increasing VARCHAR length limits compared to TEXT.
If you want to store only the first n characters and discard the rest, use explicit casting in your INSERT/UPDATE:
INSERT INTO users (email) VALUES ('verylongemail@example.com'::varchar(50));This truncates to 50 characters without raising an error. Only use this if losing data is acceptable.
If you need to store strings of variable length without constraints, change the column type to TEXT:
ALTER TABLE users ALTER COLUMN description TYPE text;PostgreSQL TEXT and VARCHAR (without length) have identical performance and no practical limit (up to ~1GB per value). This is often the best choice when length requirements are uncertain.
Add validation in your application code to check string lengths before attempting database operations. This prevents round-trips to the database and provides better user feedback:
if (email.length > 100) {
throw new Error('Email must be 100 characters or less');
}Advanced notes
PostgreSQL stores character strings as UTF-8 by default. When you specify VARCHAR(n), the n refers to character count, not byte length. For ASCII characters, 1 character = 1 byte, but multibyte characters (emoji, CJK scripts) may require 2-4 bytes per character. This means a VARCHAR(50) column can hold 50 characters regardless of encoding. The maximum possible length parameter for VARCHAR is 10,485,760. When altering a column to increase length, PostgreSQL performs a full table rewrite if the change is significant, which may take time on large tables. The error may not specify which column caused the problem in complex INSERT statements with multiple VARCHAR columns; use explicit column names in your queries and check application logs to debug. Implicit casting (space truncation) only occurs with CHAR(n), not VARCHAR(n), unless you explicitly use the ::varchar(n) cast.
Frequently asked questions
- What does "ERROR: value too long for type character varying(n)" mean?
- In PostgreSQL, VARCHAR(n) and CHARACTER VARYING(n) columns have a strict length constraint of n characters (not bytes). When you try to insert or update a row with a string that exceeds this limit, PostgreSQL raises this error to maintain data integrity. This is different from some other databases that might silently truncate values.
- How do I fix "ERROR: value too long for type character varying(n)"?
- Postgres rejects an INSERT/UPDATE when a string exceeds a VARCHAR(n) column limit. Fix by widening the column with ALTER TABLE or truncating input.
- What causes "ERROR: value too long for type character varying(n)"?
- User input or data exceeds the column's defined VARCHAR(n) length limit Application code sends strings longer than the database schema allows Data migration from a system with no length constraints to one with strict limits
- Is the "ERROR: value too long for type character varying(n)" error serious?
- It is a medium-severity error โ more of a blocker than a risk. The steps above resolve it.