Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

PostgreSQL Error 42P22: Indeterminate Collation Fix

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

PostgreSQL error 42P22 (indeterminate collation) means the database cannot decide which collation to use for a string comparison or sort, so it stops the query instead of guessing. The fix is to make the collation explicit: add a COLLATE clause to the expression, qualify the column with a collation, or set a default collation on the database or column. You do not need to reinstall PostgreSQL, and in most cases you do not need to change data.

What You'll Need

  • Access to a PostgreSQL client (psql, pgAdmin, DBeaver, or your application's SQL console) with a role that can read the affected tables.
  • The exact query or statement that produced the error, including the full error text and any DETAIL or HINT lines.
  • Permission to run ALTER TABLE or ALTER DATABASE only if you decide to change defaults; otherwise a plain SELECT is enough to test fixes.
  • Knowledge of which collation your database currently uses (check with SHOW lc_collate;).

Step-by-Step

  1. Reproduce the error and read the whole message. Run the failing statement again in a client. Error 42P22 usually appears with a line like collation "C" is not valid for encoding UTF8 or could not determine which collation to use for string comparison. The HINT often names the conflicting collations. Write down the exact expression involved, for example WHERE name = 'Alice' or ORDER BY lower(title).
  2. Check the database and column collations. Run SHOW lc_collate; and SHOW lc_ctype; to see the database defaults. Then inspect the table: SELECT column_name, collation_name FROM information_schema.columns WHERE table_name = 'your_table';. A column with collation_name set to C or POSIX while the database uses a locale such as en_US.UTF-8 is a common source of 42P22, because PostgreSQL refuses to mix them silently in comparisons.
  3. Add an explicit COLLATE clause to the expression. This is the fastest and least invasive fix. For a comparison, write WHERE name COLLATE "en_US.UTF-8" = 'Alice'. For a sort, write ORDER BY title COLLATE "C". Use a collation that exists in your installation; list them with SELECT * FROM pg_collation;. If you are unsure which one to use, match the database default from step 2.
  4. If the error comes from a function or index, fix it at the source. Expressions such as lower(column) or column || 'x' inherit collation from their inputs. If one input is a literal with no collation and another is a column with a specific collation, PostgreSQL may still be unable to decide. Apply COLLATE to the column inside the expression, or create the index with an explicit collation: CREATE INDEX idx_name ON your_table (name COLLATE "en_US.UTF-8");. Drop and recreate the index only after confirming the new definition matches your queries.
  5. Consider setting a column-level collation for permanent fixes. If a column should always use a particular collation, change it with ALTER TABLE your_table ALTER COLUMN name TYPE text COLLATE "en_US.UTF-8";. This rewrites the table and can lock it for the duration, so run it during a maintenance window and back up first. It also invalidates indexes on that column, which must be rebuilt.
  6. Check for conflicting collations across joined tables. A join between two text columns with different collations can trigger 42P22 even if each query alone works. Compare collation_name for both columns using the query in step 2. Fix by casting one side: ON a.name COLLATE "C" = b.name COLLATE "C", or by aligning the column definitions.
  7. Test the corrected query with EXPLAIN. Run EXPLAIN (VERBOSE) SELECT ... using your fixed statement. The output shows the collation chosen for each sort or comparison. If it still errors, the conflict is in a subexpression you have not covered; repeat step 1 on the inner part.
  8. Update application code or ORM settings. If the query is generated by an ORM, set the collation in the model definition or use a raw SQL fragment with COLLATE. For example, in SQLAlchemy you can use column.collate('"en_US.UTF-8"'). Do not rely on changing the database default alone if the ORM sends explicit collations.
  9. If you changed a database default, reconnect and verify. ALTER DATABASE your_db SET lc_collate = 'en_US.UTF-8'; only affects new connections and does not change existing column definitions. Reconnect your client, run SHOW lc_collate;, and retest. Existing tables keep their original collation unless altered individually.

Common Mistakes

  • Using a collation name that does not exist on the server. Always check pg_collation first; a typo produces a different error (42704) but wastes time.
  • Assuming COLLATE "C" is safe everywhere. It is valid for UTF8 databases, but mixing C with a locale collation in the same comparison still raises 42P22. Be consistent within one expression.
  • Changing a column type with ALTER TABLE without a backup. The rewrite can be slow on large tables and locks writes.
  • Forgetting that indexes carry collation. After altering a column, queries may still use an old index and behave unexpectedly until it is rebuilt.
  • Setting lc_collate at the database level and expecting existing columns to change. It does not; only new columns and new databases inherit it.

Tips

  • Prefer explicit COLLATE clauses in queries over global changes when you only need to fix one report or one join.
  • Use SELECT datcollate, datctype FROM pg_database WHERE datname = current_database(); for a quick check of the current database's collation settings.
  • When working with case-insensitive search, consider citext or a functional index with lower() plus a fixed collation, rather than relying on implicit behavior.
  • Keep a note of which collation each text column uses; this prevents surprises when adding joins or new indexes later.

Troubleshooting

If adding COLLATE to the outer expression does not clear 42P22, the conflict is likely inside a function, a view, or a subquery. Expand the query piece by piece: run the inner SELECT alone, then add the join, then the ORDER BY. The error will reappear at the exact layer that mixes collations. For views, check the view definition with \d+ view_name in psql and look for text expressions without explicit collation. If the error appears only after a PostgreSQL upgrade, compare the old and new database collation settings; a restored dump may have brought in column collations that no longer match the server default. In that case, alter the affected columns or add COLLATE clauses to the queries. If you cannot change the query (for example, it is inside a third-party extension), set the database default collation to match the column collation and reconnect. As a last resort, create a new database with the desired lc_collate and migrate the data, but only after testing on a copy.

Frequently Asked Questions

What does error 42P22 mean in plain English?

It means PostgreSQL found two or more possible collations for a text operation and refused to pick one, so it aborted the statement. You must tell it which collation to use.

Can I ignore the error and let PostgreSQL choose?

No. The error exists because guessing could produce wrong sort order or comparison results. You must specify a collation explicitly.

Does this error affect only ORDER BY?

No. It can appear in WHERE, JOIN, GROUP BY, DISTINCT, and any expression that compares or sorts text, including function calls and index creation.

Will changing the database collation fix existing tables?

No. ALTER DATABASE ... SET lc_collate affects new connections and new objects, but existing columns keep their original collation. You must alter each column or add COLLATE in queries.

Is it safe to use COLLATE "C" for everything?

It is safe for sorting and comparison if you understand that C is byte-order, not linguistic order. It can change results for case and accent handling, so test before applying it broadly.

Where can I find the list of available collations?

Run SELECT collname FROM pg_collation; in the target database. The list depends on the operating system and PostgreSQL version.