PG
PRO
01004WARNINGTier 2 — Caution✅ HIGH confidence

string data right truncation

Category: WarningVersions: All Postgres versions

What this means

SQLSTATE 01004 (warning class) is raised when a string value is silently truncated to fit the declared length of a VARCHAR(n) or CHAR(n) column. This is a warning-level notification; in some contexts Postgres raises the error-level 22001 instead.

Why it happens

  1. 1Inserting or updating a character column with a string longer than the column declaration allows

How to reproduce

Inserting an oversized string into a VARCHAR(10) column in a context where truncation is permitted.

trigger — this will ERROR
INSERT INTO products (sku) VALUES ('TOOLONGVALUE123');
WARNING: string data right truncation

Fix 1: Truncate the input explicitly before insert

When the data source can produce oversized strings.

fix
INSERT INTO products (sku) VALUES (LEFT('TOOLONGVALUE123', 10));

Why this works

LEFT() enforces the length limit in SQL before the row is written, documenting intent clearly.

Fix 2: Widen the column if the data legitimately needs more space

When the column size was set too small for actual values.

fix
ALTER TABLE products ALTER COLUMN sku TYPE VARCHAR(30);

Why this works

Resizing the column prevents truncation without data loss.

What not to do

Rely on silent truncation in production

Why it's wrong: Silent data loss can corrupt business-critical identifiers and codes.

Sources

📚 Official docs: https://www.postgresql.org/docs/current/errcodes-appendix.html

🔧 Source ref: Class 01 — Warning

Confidence assessment

✅ HIGH confidence

Standard SQL warning. See also error-class 22001 for the stricter error variant.

See also

📄 Reference pages

Character TypesLEFT()
⚙️ This error reference was generated with AI assistance and reviewed for accuracy. Examples are provided to illustrate common scenarios and may not cover every case. Always test fixes in a development environment before applying to production. Spotted an error? Suggest a correction →