How to Fix Postgres Error 42P22 Indeterminate Collation
You see postgres error 42P22 indeterminate collation when a query compares conflicting text sorting rules. You fix it by declaring one explicit sorting rule in the query or table column. Text columns store rules that control how letters sort and match. When two columns use different sorting rules, the database engine stops and raises this error. The engine refuses to guess which language rule should win during a comparison. What triggers postgres error 42P22 indeterminate collation In the of
Your coding agents run with your access.
Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.
You see postgres error 42P22 indeterminate collation when a query compares conflicting text sorting rules. You fix it by declaring one explicit sorting rule in the query or table column.
Text columns store rules that control how letters sort and match. When two columns use different sorting rules, the database engine stops and raises this error. The engine refuses to guess which language rule should win during a comparison.
What triggers postgres error 42P22 indeterminate collation
In the official error spec, code 42P22 maps to the indeterminate_collation condition. The database engine tracks this failure using the internal macro ERRCODE_INDETERMINATE_COLLATION. This error happens during text operations like sorting, grouping, or joining tables.
Every text column, expression, or string literal carries a collation property. A collation is a rule set that tells the database how to sort characters. For example, one column might use a standard C rule for fast byte sorting. Another column might use an English or German rule for local alphabet order.
When a query joins or compares these two columns, a conflict starts. Both columns have implicit sorting rules of equal rank. The query planner checks both sides and sees two different rules. Because neither rule has higher priority, the database stops the query and returns state 42P22.
This problem also appears when you combine data from different tables or views. You might migrate one table from an older server with a legacy locale. New tables on the target server might use a modern default locale. Joining the old table and the new table triggers the conflict right away.
Custom functions can also trigger this state when they take multiple text inputs. If the caller passes columns with mismatched locales, the function cannot pick one rule. String concatenation and array aggregation fail in the same way when inputs disagree.
How to diagnose and fix the failure in production
First, find the exact columns that cause the mismatch in your failing query. Look at the where clause, join conditions, order by list, and group by fields. Check the schema metadata for each table in those clauses. Inspect the collation attribute on every text, varchar, and char column involved.
You do not need superuser access to inspect table metadata in production. A read-only role can check system catalogs to see column sorting rules. In Postgres, the SELECT privilege allows reading from specific columns of a table or view. Granting read access to just the needed columns keeps sensitive data safe during diagnosis.
End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Once you find the conflicting columns, you have two safe ways to fix the problem. The fastest fix is to add an explicit collation clause to your query expression. This tells the query planner which sorting rule to use for that single comparison. You can deploy this query change in your app without locking any database tables.
The second fix is to align the column definitions in your database schema. If two columns store the same kind of data, they should share one sorting rule. You can alter the mismatched column so it uses the standard database locale. Keep in mind that changing a column type or rule rewrites indexes and locks the table.
When you run schema fixes in production, keep role permissions tight. Do not grant broad schema rights to every user on the server. The special role name PUBLIC grants a privilege to every role on the system. In Postgres 15 and later, default settings stop every user from creating objects in public.
Instead, use group roles to manage privileges when your team has many users. Grant your engineers membership in a specific group role for controlled maintenance tasks. When your team tests fixes on production data, use data masking on sensitive fields. Masking rewrites matched fields like emails, phone numbers, card numbers, and national IDs in results.
How to prevent collation mismatches across environments
Collation bugs often hide in staging and only break in production. This happens when staging and production databases start with different template locales. Always check that your staging, testing, and production clusters use the exact same default locale.
Review all schema migration scripts before they run against shared databases. Make sure new tables do not set custom column locales unless your app needs them. Standardizing on one sorting rule across all text columns stops 42P22 errors before they happen.
Add query and result controls beside Postgres
Grants and pools govern who logs into PostgreSQL. Yet every query in an open session still runs with that role's full scope. In practice, hoop.dev sits inline in the wire protocol beside your database as a sidecar for your Postgres. For every plain or prepared query an agent or human runs against production, hoop.dev decides to pass, mask, block, or escalate it for review.
When a tool or agent sends SQL, hoop.dev Postgres controls block banned statements before execution or hold risky ones for human approval via an AI Analyzer you configure. Matched sensitive columns in result rows are masked in memory before they reach the caller. Each query and decision is logged per listener, which generates evidence for SOC 2, HIPAA, and GDPR audits. View the binary on GitHub.
Frequently asked questions
What does SQLSTATE 42P22 mean in Postgres?
SQLSTATE 42P22 is the official Postgres error code for an indeterminate collation. It means a query tried to compare or sort text values with conflicting sorting rules.
Can I fix error 42P22 without changing my table schema?
Yes, you can specify the desired collation directly inside your query text. This overrides the column defaults for that single query without altering any underlying tables.
Why did my query work in staging but fail with 42P22 in production?
Your staging and production servers likely have different default database locales on specific tables. Check the column metadata in both places to find where the sorting rules diverge.
Related guides
In this post