How to fix String data length mismatch in PostgreSQL

PostgreSQLINTERMEDIATEMEDIUMUpdated Dec 26, 2025· 161 views

This error occurs when you attempt to insert or update a string value that exceeds the maximum length defined by a VARCHAR(n) or CHAR(n) column. PostgreSQL enforces strict length constraints that prevent oversized strings from being stored.

Quick fix

Fixes 22026

-- Enable detailed error messages
SET client_min_messages = DETAIL;

-- Re-run the failing query to see column details
INSERT INTO users (name) VALUES ('very long name...');
  1. Identify which column is causing the error
  2. Check the current column definition
  3. Widen the column to accommodate larger strings
  4. For bulk imports, validate data before insertion
postgresql — error
-- Enable detailed error messages
SET client_min_messages = DETAIL;
-- Re-run the failing query to see column details
INSERT INTO users (name) VALUES ('very long name...');
String data length mismatch

What this error means

PostgreSQL raises this error (SQLSTATE 22026) when the actual length of a string exceeds the maximum length constraint defined for the column. This typically happens with VARCHAR(n) and CHAR(n) data types, where n represents the maximum number of characters allowed. Unlike some databases that silently truncate oversized strings, PostgreSQL enforces this constraint strictly to maintain data integrity. The error message indicates a schema validation failure—the application or query is trying to store data that violates the column's length constraint. This can originate from web forms, bulk data imports (CSV/ETL), programmatic inserts, or schema mismatches between your application and database.

Typical symptoms

  • INSERT or UPDATE queries fail with "value too long for type character varying(n)" errors
  • Web form submissions are rejected when users enter longer input than expected
  • Bulk data imports (CSV files) fail partway through processing
  • Third-party API responses cannot be stored in existing VARCHAR columns
  • Migration scripts fail when copying data between columns with different length constraints

Common causes

  1. Input string exceeds the defined VARCHAR(n) or CHAR(n) column length
  2. Schema was changed to reduce column size without validating existing data
  3. Web form validation is missing or less strict than the database column definition
  4. CSV/bulk import contains fields longer than the target column can accept
  5. Application code passes unsanitized or unbounded user input directly to the database

How to fix "String data length mismatch"

1Identify which column is causing the error

Review your PostgreSQL error logs to find the exact query and column name:

sql
-- Enable detailed error messages
SET client_min_messages = DETAIL;

-- Re-run the failing query to see column details
INSERT INTO users (name) VALUES ('very long name...');

The error message will include the column name and its constraint.

2Check the current column definition

Query the information schema to see the current column constraints:

sql
SELECT column_name, character_maximum_length, data_type
FROM information_schema.columns
WHERE table_name = 'users' AND column_name = 'name';

This shows the actual VARCHAR(n) or CHAR(n) limit.

You can also use \d table_name in psql for a quick view.

3Widen the column to accommodate larger strings

The most common solution is to increase the column's maximum length:

sql
ALTER TABLE users ALTER COLUMN name TYPE VARCHAR(500);

In PostgreSQL 12+, changing VARCHAR length upward is a metadata-only operation and completes instantly without rewriting the table. Choose a length that accommodates your data with some buffer.

4For bulk imports, validate data before insertion

If you're importing data from CSV or an external source, use a staging table with looser constraints:

sql
-- Create a staging table with TEXT (no length limit)
CREATE TABLE users_staging AS
SELECT * FROM users WHERE FALSE;

ALTER TABLE users_staging ALTER COLUMN name TYPE TEXT;

-- Import data into staging table
COPY users_staging FROM stdin;

-- Clean and validate before moving to production
INSERT INTO users (name)
SELECT SUBSTRING(name, 1, 500) FROM users_staging
WHERE LENGTH(name) <= 500;
5Consider using TEXT if length is unbounded

If you don't have strict length requirements, use TEXT instead of VARCHAR(n):

sql
ALTER TABLE users ALTER COLUMN name TYPE TEXT;

TEXT accepts strings of any length up to 1 GB and performs identically to VARCHAR. If you need to enforce a maximum for business reasons, use a CHECK constraint instead:

sql
ALTER TABLE users
ADD CONSTRAINT name_length_check CHECK (LENGTH(name) <= 500);
6Add application-level validation

Prevent the error from happening by validating string length in your application before sending to PostgreSQL:

javascript
// Node.js / JavaScript example
const MAX_NAME_LENGTH = 500;

if (userData.name && userData.name.length > MAX_NAME_LENGTH) {
  throw new Error(`Name must be ${MAX_NAME_LENGTH} characters or less`);
}

await prisma.user.create({ data: userData });

This provides better user feedback and reduces unnecessary database round-trips.

Advanced notes

PostgreSQL's strict string length enforcement differs from some other databases that silently truncate oversized strings. This is intentional—it follows the SQL standard and catches data integrity issues early.

For migration scenarios where you're copying data between columns with different lengths, use SUBSTRING() or LEFT() to truncate before insertion:

sql
INSERT INTO new_table (short_name)
SELECT SUBSTRING(old_table.long_name, 1, 50) FROM old_table;

If you're using an ORM like Prisma or SQLAlchemy, ensure your schema validation (Zod, Yup, Pydantic, etc.) matches your database column definitions to catch mismatches early in the application layer.

Frequently asked questions

What does "22026" mean?
PostgreSQL raises this error (SQLSTATE 22026) when the actual length of a string exceeds the maximum length constraint defined for the column. This typically happens with VARCHAR(n) and CHAR(n) data types, where n represents the maximum number of characters allowed. Unlike some databases that silently truncate oversized strings, PostgreSQL enforces this constraint strictly to maintain data integrity.
How do I fix "22026"?
This error occurs when you attempt to insert or update a string value that exceeds the maximum length defined by a VARCHAR(n) or CHAR(n) column. PostgreSQL enforces strict length constraints that prevent oversized strings from being stored.
What causes "22026"?
Input string exceeds the defined VARCHAR(n) or CHAR(n) column length Schema was changed to reduce column size without validating existing data Web form validation is missing or less strict than the database column definition
Is the "22026" error serious?
It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.

Official resources & further reading