PostgreSQL Error 22005: How to Fix Assignment Type Mismatch
In this article
PostgreSQL error 22005 occurs when you try to assign a value to a column or variable with an incompatible data type. This is not a syntax error—it’s a type coercion failure. The database rejects the assignment because the source value cannot be safely converted to the target type, such as trying to insert text into an integer field or a date into a boolean. Fixing this requires identifying the mismatched data types and correcting them at the source. No external tools are needed—only access to your PostgreSQL database and awareness of your schema.
What You'll Need
- Access to a PostgreSQL database (via psql, pgAdmin, or any SQL client)
- Knowledge of the table schema (column names and data types)
- The exact SQL statement that triggered the error
- Ability to view or export query logs if the error occurs in an application
Step-by-Step
- Locate the exact SQL statement that caused the error. Check your application logs, psql output, or database error logs. The error message will typically include the line number and the problematic value, such as:
ERROR: invalid input syntax for type integer: "abc". - Identify the target column in your table. Use
\d tablenamein psql or open the table structure in pgAdmin to see each column’s data type. Note which column the assignment was trying to make—this is the column with the incompatible type. - Examine the value being assigned. Compare it to the expected type. For example, if the column is INTEGER, ensure the value is a whole number like 42, not "42", "forty-two", or NULL without proper handling.
- Check for implicit type conversions. PostgreSQL does not automatically convert strings to numbers or dates unless they are in a recognized format. If your query uses a string like
'2023-01-01'in a DATE column, that’s valid. But if you pass'January 1, 2023'to a DATE column without casting, it may fail depending on locale settings. - Verify NULL handling. If your column does not allow NULL and your query passes NULL without a default, the assignment fails. Use
COALESCE(value, default_value)or ensure your application never sends NULL to non-nullable columns. - Use explicit casting if needed. If the value is correct but needs type conversion, use PostgreSQL’s cast syntax:
CAST('123' AS INTEGER)or the shorthand'123'::INTEGER. This makes intent clear and avoids ambiguity. - Test the corrected query manually. Run the updated SQL directly in psql or your client. Example: if you were inserting
INSERT INTO users (age) VALUES ('not a number');, change it toINSERT INTO users (age) VALUES (25);. - Update your application code. If the error occurs in an app (like Python, Node.js, or Java), trace how the value is built. Ensure variables are typed correctly before being passed to the SQL query. Use parameterized queries and avoid string concatenation to prevent type confusion.
- Re-run the operation. After fixing the query and updating the application, execute the operation again. The error should disappear if the types now match.
Common Mistakes
- Assuming PostgreSQL will auto-convert any string to a number. It only accepts strings that match exact numeric formats.
- Passing user input directly into SQL without validation. A text field from a web form might contain letters or symbols that break integer or date assignments.
- Using the wrong column order in INSERT statements. If you omit column names and rely on order, a string value might end up in an integer column by accident.
- Copying data from spreadsheets or CSVs with hidden whitespace or formatting. A value like " 123 " or "123.0" may cause errors in integer columns.
- Confusing TIMESTAMP with DATE. A timestamp string like "2023-06-15 14:30:00" cannot be assigned to a DATE-only column unless truncated.
Tips
- Always use explicit column names in INSERT and UPDATE statements to avoid order-related errors.
- Use
SELECT pg_typeof(your_column) FROM your_table LIMIT 1;to confirm the actual data type of a column during debugging. - For debugging, wrap suspicious values in
quote_nullable()to see their exact representation in logs. - Validate data types in your application layer before sending queries to the database. A type check in code is faster and more user-friendly than a database error.
- Use constraints like
CHECK (age > 0)to catch invalid values early, even if they’re the right type. - When importing data, use
COPYwithFORMAT CSVand let PostgreSQL handle type coercion—it’s more robust than manual INSERTs.
Troubleshooting
If the error persists after following the steps above, check these areas:
- Review the full error message. PostgreSQL often includes the exact value that failed. Copy it exactly and test it in a standalone query:
SELECT 'your_value'::target_type;to see if it’s valid. - Check for triggers or rules on the table. Sometimes a trigger modifies the value after your INSERT/UPDATE and causes a secondary type error. Use
\d+ tablenameto list triggers. - Verify collation and locale settings. If the value is a date or timestamp, your database’s LC_TIME setting might reject formats that work elsewhere. Try setting
SET lc_time TO 'C';temporarily to test. - Test with a minimal query. Isolate the problem by creating a new table with one column and inserting just the problematic value. If it works there, the issue is elsewhere in your schema or logic.
- Check for application-level ORM misconfigurations. Frameworks like Django or Hibernate may map fields incorrectly. Confirm your model’s field type matches the PostgreSQL column type.
- Look at the transaction context. If you’re running multiple statements in a transaction, the error might originate from a prior statement that corrupted the data. Roll back and run statements one by one.
Frequently Asked Questions
Why does PostgreSQL reject "123" as an integer?
It doesn’t—unless "123" is stored as a string with invisible characters, or the column is not INTEGER but something like BOOLEAN or DATE. Verify the column type and trim whitespace: TRIM(' 123 ')::INTEGER.
Can I disable type checking to avoid this error?
No. PostgreSQL enforces type safety by design. Disabling it would compromise data integrity. Always fix the source data or cast explicitly.
Why does the same query work in one database but not another?
Different locale settings, PostgreSQL versions, or schema definitions (e.g., one column is TEXT, another is INTEGER) can cause this. Compare the table structures and database parameters between environments.
Does this error happen with JSON columns?
Yes, if you try to assign a non-JSON string to a JSON column. Use jsonb ' {"key": "value"} ' or cast with ::jsonb. Plain text like "hello" is not valid JSON.
How do I find all columns in my database that might cause this error?
Run SELECT table_name, column_name, data_type FROM information_schema.columns WHERE data_type IN ('integer', 'numeric', 'date', 'boolean'); and audit all INSERT/UPDATE statements targeting these columns.
Can a foreign key cause error 22005?
No—foreign key errors are 23503 or 23507. But if the foreign key column is INTEGER and you try to insert a string, you’ll get 22005 before the foreign key constraint is even checked.