Incident Response

PostgreSQL ERROR: operator does not exist: integer = text

PostgreSQL will not quietly compare a number with a string. Here is why the same query works with a literal and fails with a parameter, which side to cast so your index still works, and what changed in the error message in PostgreSQL 19.

· 12 min read · AG Data Team

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

  1. 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;
  2. 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[]);
  3. 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.
  4. Fix the schema when the column is simply wrong. An ID stored as varchar in one table and integer in 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:

Key takeaways

PostgreSQL Errors SQL Performance
Work with us

Type mismatches slowing down or breaking production?

We find the queries that compare the wrong types, fix them without losing index use, and plan safe column type changes on large tables.

Get in touch More from the blog