ERROR: operator does not exist: integer = text means PostgreSQL was asked to compare two values of different types and found no = operator that accepts that pair. It does not guess. You either cast one side explicitly or, better, make both sides the same type. Where you put that cast decides whether the query keeps using its index.
=> SELECT * FROM orders WHERE customer_id = '42'::text;
ERROR: operator does not exist: integer = text
LINE 1: SELECT * FROM orders WHERE customer_id = '42'::text;
^
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
The same error appears in many shapes: character varying = integer, uuid = text, bigint = character varying, text = integer[]. The SQLSTATE is 42883 (undefined_function in the error codes table), the same code as the closely related "function does not exist" error, because PostgreSQL resolves operators much like functions.
Why it worked before (or in another database)
MySQL converts strings and numbers for you in comparisons. PostgreSQL used to do something similar for text: until 8.3, most non-character types were automatically cast to text when an operator needed text. The PostgreSQL 8.3 release notes removed that, because those automatic casts "too often caused surprising behavior". Their example is current_date < 2017-11-17, which used to compare a date with the integer 1989 as text and returned nonsense. Since then, mixing a number and a string needs an explicit cast. char(n) and varchar(n) still convert to text automatically, and || still accepts non-text input as long as one side is a string.
Why a literal works and a parameter fails
This is what confuses most people. A quoted value in the SQL text has no type yet; it is unknown. The operator type resolution rules say that if one argument of a binary operator is unknown, PostgreSQL assumes it has the same type as the other side. So customer_id = '42' becomes integer = integer, and it works.
A value that already has a type gets no such treatment. A column, an explicit '42'::text, or a bind parameter the driver declared as text or varchar all arrive typed, and there is no integer = text operator to use.
-- works: '42' is an untyped literal, resolved as integer
SELECT * FROM orders WHERE customer_id = '42';
-- fails: the right side already has a type
SELECT * FROM orders WHERE customer_id = '42'::text;
SELECT * FROM orders o JOIN legacy_import l ON o.customer_id = l.customer_ref; -- varchar column
That is why a query copied from the application log runs fine in psql but fails from the application: in psql you pasted a literal, while the driver sent a typed parameter. A common case in Java: the PostgreSQL JDBC driver sends values set with setString() as varchar by default. Its documentation describes the stringtype=unspecified connection parameter, which sends them untyped so the server infers the type. That is a workaround for legacy code; calling setInt() or setObject() with the right type is the proper fix.
How to fix it
- Find out the real types. Check both sides before you cast anything.
SELECT pg_typeof(customer_id) FROM orders LIMIT 1; SELECT table_name, column_name, data_type FROM information_schema.columns WHERE column_name IN ('customer_id', 'customer_ref') ORDER BY table_name; - Cast the value, not the column. Put the cast on the parameter or literal so the column stays as it is and its index can still be used.
-- cast the value, not the column: the index on customer_id still works SELECT * FROM orders WHERE customer_id = $1::integer; -- a join between mismatched columns: cast the side you filter less on SELECT ... FROM orders o JOIN legacy_import l ON o.customer_id = l.customer_ref::integer; -- arrays: the array type must match the column too SELECT * FROM orders WHERE customer_id = ANY($1::integer[]); - Fix the type in the application. Bind integers as integers, UUIDs as UUIDs. In ORMs this usually means fixing the model field type so it matches the column.
- Fix the schema when the column is simply wrong. An ID stored as
varcharin one table andintegerin another will keep producing this error in every new join.-- the real fix: make the column the right type -- (rewrites the table and holds ACCESS EXCLUSIVE for the whole rewrite) ALTER TABLE legacy_import ALTER COLUMN customer_ref TYPE integer USING customer_ref::integer;
Do not "fix" it with CREATE CAST ... AS IMPLICIT. The CREATE CAST documentation advises being conservative about implicit casts: too many implicit paths make PostgreSQL choose surprising interpretations or fail with ambiguity errors, and cross-category casts such as text to int4 are best kept explicit-only. Adding one to silence an error trades a clear failure for subtle bugs.
PostgreSQL 19: a more useful message
In PostgreSQL 19 the parser source splits the hint into a DETAIL and a HINT, and it tells two different failures apart:
-- PostgreSQL 19 splits it into DETAIL and HINT
ERROR: operator does not exist: integer = text
DETAIL: No operator of that name accepts the given argument types.
HINT: You might need to add explicit type casts.
If an operator with that name exists but its schema is not on your search_path, PostgreSQL 19 says so: "An operator of that name exists, but it is not in the search_path." If no operator with that name exists at all, the detail is "There is no operator of that name." Older versions give the generic "No operator matches the given name and argument types" hint in both cases. This matters for operators from extensions, for example ones installed into a separate extensions schema, where the argument types were never the problem.
On large production databases
On a small test database a wrongly placed cast costs nothing, so these problems surface only when the table is big:
- A cast on the column disables the plain index.
WHERE customer_id::text = '42'compiles, but a B-tree oncustomer_idcannot serve it. Per the expression index documentation, only an index on the same expression could. On a table with hundreds of millions of rows that is the difference between a millisecond index lookup and a full sequential scan. Check withEXPLAIN; our slow query tuning checklist covers what to look for. - Mismatched join keys hurt even with a cast. Joining
integertovarcharforces a cast on one side for every row. Cast the side that is not used for index lookups, or better, change the column type. - Changing a column type rewrites the table.
ALTER COLUMN ... TYPE integerholds anACCESS EXCLUSIVElock while it rewrites. On a large table, plan it like any big migration: add a new column, backfill in batches, swap it in, then drop the old one. It also fails halfway if a single row cannot be converted, so check first with a query for values that are not valid integers. - Find all the places at once. In our experience the same mismatch hides in many queries. Search the logs for SQLSTATE
42883(with%einlog_line_prefix), and look for::textcasts on indexed columns inpg_stat_statements. - Test with production-like data and drivers. The error comes from the parameter types the driver sends, so test through the real application stack, not by pasting SQL into psql.
Key takeaways
- PostgreSQL has no implicit cast between numbers and text since 8.3, so
integer = texthas no operator. - Untyped literals adapt to the other side; typed parameters and columns do not. That is why psql works and the app fails.
- Cast the value, not the indexed column, or the index cannot be used.
- Fix types in the application and schema; avoid implicit casts created just to hide the error.
- PostgreSQL 19 tells you when the operator exists but is outside your
search_path.