Common SQL Errors

PostgreSQL Error - 1004 string_data_right_truncation Error Explained and Fixed

August 4, 2025

string_data_right_truncation (SQLSTATE 22001) appears when a value longer than the defined column length is inserted or updated.

Sign up for the latest in common SQL errors from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

What is string_data_right_truncation?

string_data_right_truncation occurs in PostgreSQL when your data is longer than the target column’s length. Trim or expand the column, then retry the statement to resolve the error.

Error Highlights

Typical Error Message

string_data_right_truncation

Error Type

Data Truncation Error

Language

PostgreSQL

Symbol

string_data_right_truncation

Error Code

1004

SQL State

Explanation

Table of Contents

What is the PostgreSQL string_data_right_truncation error?

PostgreSQL raises string_data_right_truncation (SQLSTATE 22001) when an INSERT, UPDATE, or COPY statement supplies a string that exceeds the defined length of a CHAR, VARCHAR, or TEXT column with a CHECK length constraint.

The server cancels the statement to prevent silent cutting of characters. The problem must be fixed in the client SQL or the schema before the transaction can succeed.

What Causes This Error?

Column length limits are the primary trigger.

A VARCHAR(50) column cannot accept a 60-character string, so PostgreSQL throws the error immediately.

Implicit casts can also cause it. Assigning a long TEXT literal to a shorter VARCHAR variable inside PL/pgSQL has the same effect.

CHECK constraints that call length() can replicate this error because they manually enforce a length cap.

How to Fix string_data_right_truncation

First, identify the offending column by reading the DETAIL line in psql or the server log.

Then choose one of two routes: shorten the data or enlarge the column.

To shorten, TRIM, SUBSTRING, or LEFT() the value in your DML. To enlarge, run ALTER TABLE ... ALTER COLUMN ... TYPE VARCHAR(new_length) or TEXT.

Common Scenarios and Solutions

User-entered free-form comments often overflow VARCHAR(255). Move the column to TEXT or cut the input in the application layer.

ETL loads from CSV files may have rogue long strings.

Add a preprocessing step that truncates columns or set the column to TEXT before the COPY.

Best Practices to Avoid This Error

Define realistic length limits based on actual profiling, not guesses.

Oversized data is less disruptive than application crashes.

Add CHECK (char_length(col) <= limit) only when business logic demands it, and test edge-case inputs with unit tests or Galaxy’s shared queries.

Related Errors and Solutions

ERROR: value too long for type character varying - the same root cause, reported with the specific column type.

ERROR: character not in repertoire - thrown when encoding fails, sometimes alongside truncation when multibyte characters are miscounted.

.

Common Causes

Related Errors

FAQs

Does this error corrupt data?

No. PostgreSQL aborts the statement before writing anything, so existing rows stay intact.

Can I ignore the error and let PostgreSQL truncate?

No. PostgreSQL never silently truncates right-side characters. You must fix the data or the schema.

Is TEXT always safe?

TEXT removes length limits but you may still hit disk or application constraints. Use TEXT when you genuinely need unbounded strings.

How does Galaxy help?

Galaxy’s AI copilot flags length mismatches during query drafting and its shared editor lets teams review schema changes, preventing runtime errors.

Start Querying with the Modern SQL Editor Today!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Check out some other errors

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo