Raised when an inserted or updated string exceeds the target column length.
PostgreSQL string_data_right_truncation happens when a value is longer than the target column’s length. Trim or cast the value, enlarge the column with ALTER TABLE … TYPE, or validate data before insert to fix the error.
PostgreSQL Error 22001 (string_data_right_truncation)
PostgreSQL raises the string_data_right_truncation error when an INSERT, UPDATE, or COPY statement supplies a string longer than the defined length of the target column.
The database refuses to silently cut the data, protecting integrity.
You must resize the column, shorten the value, or cast it to a compatible type before the query can succeed.
The immediate trigger is a mismatch between the string length and the column’s maximum length defined by VARCHAR(n), CHAR(n), or TEXT with a CHECK constraint.
The error can surface in application code, ad-hoc SQL, bulk loads, or ETL pipelines when validation is missing or schema changes lag behind data growth.
First confirm which column fails by reviewing the full error stack or enabling client_min_messages = DETAIL.
Then decide to resize the column, trim the data, or reject invalid rows.
Use ALTER TABLE … ALTER COLUMN … TYPE to widen the column, or use substring(), LEFT(), or CAST to fit the value before insertion.
Long user input in a web form can overflow a VARCHAR(50) username column. Expanding the column to VARCHAR(100) solves the issue without data loss.
When importing CSV files, a rogue field may exceed its expected size.
Adding a staging table with looser types lets you clean data before moving it to production tables.
Validate and trim input at the application layer. Keep schema and business requirements in sync and use CHECK(LENGTH(col) <= n) instead of hard VARCHAR limits when lengths may change.
Monitor errors with pg_stat_activity or log_min_error_statement so you can react quickly and adjust schema or data pipelines.
numeric_value_out_of_range occurs for numeric overflow.
not_null_violation fires when a NOT NULL column receives NULL. Both follow similar diagnosis steps: inspect the failing data, adjust schema or sanitize inputs.
.
The error message usually names the column. If not, enable log_error_verbosity = verbose or examine the DETAIL line in the server log.
TEXT removes length limits and therefore avoids this error, but you should still add CHECK constraints if business rules require maximum sizes.
Changing a VARCHAR length upward is metadata only in PostgreSQL 12+. The operation is instantaneous and does not rewrite the entire table.
Galaxy’s AI copilot highlights column lengths as you type and suggests ALTER TABLE commands, reducing trial-and-error and preventing truncation errors before execution.