PostgreSQL Error 2200G: Type Mismatch Fix
In this article
Error 2200G in PostgreSQL is the "most specific type mismatch" error, raised when a query or command supplies a value whose type does not match what the target column, function, or operator expects. The fix is to make the types line up: cast the value explicitly, change the column type, or adjust the query so both sides of a comparison use the same type. This guide walks through diagnosing the exact mismatch and correcting it without breaking existing data.
What You'll Need
- Access to the PostgreSQL server with a role that can read the relevant tables and run
EXPLAINor\dcommands. - The full error message, including the
DETAILandHINTlines if your client shows them. - A client such as
psql, pgAdmin, DBeaver, or any tool that displays server errors verbatim. - Backup or a transaction you can roll back before altering any column type.
Step-by-Step
- Read the whole error, not just the code. PostgreSQL usually prints a message like
ERROR: 2200G: most specific type mismatchfollowed byDETAILnaming the two types involved andHINTsuggesting a cast. The detail line is the fastest route to the fix. If your client truncates errors, run the same statement inpsqlso you see every line. - Identify the offending statement. Look at the SQL that failed. Error 2200G commonly appears in
INSERT,UPDATE,SELECT ... WHERE,UNION,CASE, and function calls where an argument type does not match the declared parameter type. Note every literal, parameter placeholder, and column reference in that statement. - Inspect the target column or function signature. Run
\d table_nameinpsql(or open the table in your GUI) to see the declared type of each column. For a function, run\df function_nameto see the argument types. Compare those declared types with the types of the values you are supplying. - Check the actual type of each value. In
psql, useSELECT pg_typeof(your_value);to see how PostgreSQL parsed it. A quoted literal like'123'istext, an unquoted123isinteger, and a parameter sent by a driver may arrive astexteven when the column isinteger. This step usually reveals the mismatch immediately. - Apply an explicit cast. If the column is
integerand the value is text, writeCAST('123' AS integer)or the shorthand'123'::integer. For dates, use'2024-01-15'::date. For UUIDs, use'...'::uuid. Explicit casts tell PostgreSQL exactly which type you intend and resolve most 2200G errors. - Fix comparisons with mismatched types. If the error comes from a
WHEREclause, cast the literal rather than the column:WHERE id = '42'::integerinstead ofWHERE id::text = '42'. Casting the column can prevent index use and slow queries. If both sides are columns of different types, cast the narrower or less selective one. - Handle
UNION,CASE, andCOALESCE. These constructs require compatible types across branches. If one branch returnsintegerand another returnstext, cast both to a common type such astextornumeric. ForCASE, cast the result expression or each branch explicitly. - Check driver and parameter binding. If the error only appears from an application, the driver may be sending parameters as text. In many drivers you can set the parameter type explicitly or cast inside the SQL. For example, in a prepared statement, write
$1::integerin the query so the server knows the intended type. - Consider changing the column type if the mismatch is structural. If a column was created as
textbut should hold numbers, alter it:ALTER TABLE table_name ALTER COLUMN column_name TYPE integer USING column_name::integer;. TheUSINGclause is required when PostgreSQL cannot cast implicitly. Run this inside a transaction, check for rows that fail to cast, and roll back if needed. - Re-run the statement and verify. Execute the corrected query. If it succeeds, run a small
SELECTto confirm the stored values look right. ForUPDATEorINSERT, check the affected row count matches what you expected. - Search the codebase for similar patterns. Error 2200G often appears in more than one place, especially in applications that build SQL dynamically. Grep for the column name or the query fragment and fix every occurrence so the error does not resurface elsewhere.
Common Mistakes
- Casting the column instead of the literal.
WHERE created_at::text = '2024-01-15'works but disables the index oncreated_at. Cast the literal to the column type instead. - Assuming quoted numbers are numbers. In PostgreSQL,
'123'is text. Without a cast, comparing it to an integer column can trigger 2200G or an implicit-cast surprise. - Forgetting the
USINGclause.ALTER COLUMN ... TYPEfails when no implicit cast exists. Always includeUSING column_name::new_typefor text-to-number or text-to-date changes. - Ignoring the
DETAILline. The error code alone is vague; the detail names the exact types. Skipping it leads to guesswork and repeated failures. - Changing types without checking data. A column containing
'N/A'cannot be cast tointeger. Query for non-castable rows first:SELECT * FROM t WHERE column_name !~ '^[0-9]+$';.
Tips
- Use
pg_typeof()liberally when debugging; it removes guesswork about how PostgreSQL parsed a value. - Prefer the
::shorthand for readability, but useCAST(... AS ...)when the expression is complex. - When writing migrations, add explicit casts in the
USINGclause and test on a copy of production data first. - If a driver sends everything as text, set the parameter type in the driver configuration where supported, or cast in SQL with
$1::type. - Keep the original error text when asking for help; the
DETAILandHINTlines are what experienced users need to diagnose quickly.
Troubleshooting
If the error persists after adding a cast, check that you cast the right expression. A cast on the wrong side of a comparison or on an outer expression does not help. Re-read the DETAIL line and confirm the two types it names match what you changed.
If the error appears only in a function call, run \df+ function_name to see every overload. PostgreSQL may be choosing an overload you did not expect. Cast the argument to the exact type of the overload you want.
If an ALTER COLUMN ... TYPE fails, run it inside BEGIN; ... ROLLBACK; first to see the error without changing data. Common causes are non-castable values, dependent views, or indexes that need rebuilding. Drop and recreate dependent views after the change if necessary.
If the error comes from an ORM or query builder, inspect the generated SQL. Many ORMs send parameters as text by default; configure the column type in the model or add a cast in a raw fragment.
If nothing works, reduce the statement to the smallest failing form: a single SELECT with one comparison. That isolates the mismatch and makes the fix obvious.
Frequently Asked Questions
What does PostgreSQL error 2200G mean?
It means the most specific type PostgreSQL could infer for a value does not match the type required by the column, operator, or function. The fix is an explicit cast or a type change.
Is 2200G the same as a datatype mismatch error?
It is a specific datatype mismatch code. Other codes such as 42804 cover related cases, but 2200G typically appears when a value's inferred type conflicts with the target.
Can I fix 2200G without changing the table?
Yes. In most cases, casting the literal or parameter in the query resolves it. Changing the column type is only needed when the stored type is structurally wrong.
Why does the error only happen from my application, not in psql?
Drivers often send parameters as text. Cast the parameter in SQL with $1::type or configure the driver to send the correct type.
Does casting a column break indexes?
Casting a column in a WHERE clause usually prevents normal index use. Cast the literal instead, or create an expression index if you must cast the column.
What is the safest way to change a column type?
Back up the table, test the cast with a SELECT first, then run ALTER TABLE ... ALTER COLUMN ... TYPE ... USING ... inside a transaction so you can roll back if it fails.