PostgreSQL error 22000 occurs when data fails validation during insertion, update, or type conversion. Common causes include string truncation, numeric overflow, or invalid value representations that violate column constraints.
A data exception (error code 22000) is PostgreSQL's generic error for data-related constraint violations. It indicates that the data you're trying to insert, update, or cast doesn't conform to the expected format, type, or size of the target column. This is a class-level error under the SQL standard Class 22, which encompasses various specific data conditions like string truncation (22001), numeric overflow (22003), null constraint violations (22004), and invalid type representations (22P02). The database engine has rejected the operation to maintain data integrity.
Examine PostgreSQL logs and your application error output to identify which query caused the error. Run the query directly in psql with SET client_min_messages = NOTICE; to get more detailed feedback:
SET client_min_messages = NOTICE;
SELECT ...; -- Your failing queryThe error message should include additional context about which column or operation failed.
Use the \d meta-command in psql to view the table structure:
\d table_name;Check the data types, size limits (VARCHAR length), NOT NULL constraints, and any CHECK constraints. For example:
- VARCHAR(20) means the column accepts only up to 20 characters
- NUMERIC(5,2) means 5 total digits with 2 after the decimal point
- NOT NULL means the column cannot accept NULL values
Before running INSERT or UPDATE statements, verify the data in your application code:
// JavaScript example
const firstName = "John Doe Extension";
if (firstName.length > 20) {
console.error("First name exceeds 20 character limit");
firstName = firstName.substring(0, 20); // Truncate explicitly
}Always validate:
- String length against column VARCHAR limits
- Numeric ranges (check if value fits in the defined precision)
- Required vs optional fields (NULL handling)
- Date formats match PostgreSQL expectations (YYYY-MM-DD)
If the error occurs during type casting, explicitly convert values before attempting the cast:
-- BAD: Direct cast of potentially invalid string
SELECT CAST(user_input AS INTEGER);
-- GOOD: Validate the string format first or use error handling
SELECT CAST(TRIM(user_input) AS INTEGER);
-- In PL/pgSQL, use exception handling
BEGIN
SELECT CAST(user_input AS INTEGER) INTO numeric_var;
EXCEPTION WHEN invalid_text_representation THEN
RAISE NOTICE 'Invalid numeric format: %', user_input;
END;Use NULLIF() to handle edge cases like division by zero:
SELECT amount / NULLIF(quantity, 0) AS price_per_unit;If the issue is that your column is too small or has wrong constraints, modify the schema:
-- Increase string column size
ALTER TABLE users ALTER COLUMN first_name TYPE VARCHAR(50);
-- Change numeric precision
ALTER TABLE products ALTER COLUMN price TYPE NUMERIC(10,2);
-- Add/remove NOT NULL constraint
ALTER TABLE orders ALTER COLUMN customer_id DROP NOT NULL;
ALTER TABLE orders ALTER COLUMN total_amount SET NOT NULL;Be cautious with these changes in production—they may require data migration if tightening constraints.
PostgreSQL error 22000 is a broad category. More specific error codes within Class 22 include: 22001 (string_data_right_truncation), 22003 (numeric_value_out_of_range), 22004 (null_value_not_allowed), 22008 (datetime_field_overflow), 22012 (division_by_zero), 22P02 (invalid_text_representation), 22P03 (invalid_binary_representation). When catching exceptions in application code, always check the SQLSTATE code (e.g., via psycopg2.errors or similar libraries) rather than parsing error message text, as messages are localized and less stable across PostgreSQL versions. In PL/pgSQL procedures, use the exception clause with named conditions: BEGIN ... EXCEPTION WHEN data_exception THEN ... END; For XML operations, related errors include 2200L (invalid_xml_document) and 2200M (invalid_xml_content), which also fall under Class 22.
ERROR: syntax error at end of input
Syntax error at end of input in PostgreSQL
Bind message supplies N parameters but prepared statement requires M
Bind message supplies N parameters but prepared statement requires M in PostgreSQL
Multidimensional arrays must have sub-arrays with matching dimensions
Multidimensional arrays must have sub-arrays with matching dimensions
ERROR: value too long for type character varying
Value too long for type character varying
insufficient columns in unique constraint for partition key
How to fix "insufficient columns in unique constraint for partition key" in PostgreSQL