Solving the PostgreSQL 'value too long for type character varying' Error

beginner🐘 PostgreSQL2026-04-12| PostgreSQL (all versions), Linux/Docker/Windows, Node.js/Python/PHP backend environments

Error Message

ERROR: value too long for type character varying(255)
#postgresql#varchar#database-design#sql-errors#backend

The Scenario

Last week, I was integrating a third-party OAuth provider into a production app. Everything passed our local tests using short mock tokens. However, the moment we went live, a user with a particularly heavy payload triggered a crash. The logs showed a familiar, frustrating wall of text:

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

This error occurs when you attempt to insert a string that exceeds the hard-coded limit of a VARCHAR(n) column. In my case, a system-generated tracking URL reached 312 characters, but the database was strictly capped at 255.

Analysis: Why PostgreSQL is so strict

PostgreSQL prioritizes data integrity over convenience. Unlike some legacy databases that might silently truncate your data to make it fit, Postgres refuses to guess which part of your data is expendable. If you define a column as character varying(255), the database guarantees that no row will ever exceed that limit.

You will likely encounter this bottleneck when handling:

  • External API data: Webhooks or JSON blobs that grow as providers add new features.
  • Long-form text: User bios or comments where a '255' limit was an arbitrary guess.
  • Technical strings: Encrypted tokens, session IDs, or deeply nested file paths.
  • Legacy Migrations: Moving data from systems with 'silent truncation' into the stricter Postgres environment.

Step 1: Identify the Culprit

If your application logs are vague, you need to inspect the table structure directly. Run this command in your psql terminal or any database GUI like DBeaver:

\d table_name;

Look for the Type column. If you see character varying(255), that is your bottleneck. For a more detailed view across your entire database, you can query the information schema:

SELECT column_name, character_maximum_length 
FROM information_schema.columns 
WHERE table_name = 'your_table_name';

Step 2: The Quick Fix (Increasing the Limit)

If you know exactly how much space you need—say, 500 characters instead of 255—you can simply bump the limit. One of PostgreSQL's strengths is that increasing the length of a VARCHAR column is usually a metadata-only change. It doesn't require a slow, full-table rewrite.

ALTER TABLE users 
ALTER COLUMN email TYPE varchar(500);

Note: While this is fast, it still requires an ACCESS EXCLUSIVE lock. On a table with millions of rows, this lock only lasts milliseconds, but it will briefly pause other incoming queries.

Step 3: The Best Practice (Switching to TEXT)

Stop chasing the limit. If you find yourself repeatedly moving from 255 to 500, and then to 1000, you should switch to TEXT. In PostgreSQL, VARCHAR(n), VARCHAR, and TEXT use the same underlying storage format. There is zero performance penalty for using TEXT.

I now use TEXT for almost everything that isn't a fixed-length code. It supports strings up to 1GB in size.

ALTER TABLE users 
ALTER COLUMN bio TYPE TEXT;

By switching to TEXT, you move validation to your application code. This is much easier to update than a database schema and prevents your database from being the point of failure during a traffic spike.

Step 4: The Emergency Patch (Truncation)

Sometimes you can't run a migration immediately because of a frozen production environment. In those cases, you must trim the data before it hits the database. This prevents the 500 error but results in data loss, so use it carefully.

Example in Python:

# Truncate to 255 characters to satisfy the DB constraint
safe_description = raw_input[:255]
cursor.execute("INSERT INTO items (description) VALUES (%s)", (safe_description,))

Verification: Confirming the Fix

After running your ALTER TABLE command, double-check the schema to ensure the change stuck:

\d users;

The column should now list text or your new character varying(500) limit. Finally, test it with a real-world string that previously failed:

-- This should now succeed
INSERT INTO users (email) 
VALUES ('test-' || repeat('a', 300) || '@example.com');

Summary Checklist

  • Specific limits: Use ALTER COLUMN TYPE varchar(new_size) for minor adjustments.
  • Future-proofing: Use TEXT to avoid ever dealing with this error again.
  • Performance: Don't worry—TEXT is just as fast as VARCHAR in Postgres.
  • User Experience: Always validate string lengths in your frontend or API to show helpful errors rather than raw database crashes.

Related Error Notes