How to fix Indeterminate collation in PostgreSQL
PostgreSQL cannot determine which collation to use when comparing columns with conflicting implicit collations. Resolve by explicitly specifying a collation using the COLLATE clause.
Quick fix
Fixes Indeterminate collation
SELECT column_name, collation_name
FROM information_schema.columns
WHERE table_name = 'your_table_name'
AND collation_name IS NOT NULL;- Identify the conflicting collations
- Use the COLLATE clause to specify collation explicitly
- Fix UNION queries with collation mismatches
- Standardize collations in your schema
SELECT column_name, collation_nameFROM information_schema.columnsWHERE table_name = 'your_table_name'AND collation_name IS NOT NULL;Indeterminate collation
What this error means
PostgreSQL uses collations to determine how strings are sorted and compared, including language-specific rules for alphabetical ordering and case sensitivity. When the database encounters conflicting non-default implicit collations among input expressions, it cannot automatically determine which collation rules to apply. This happens most commonly in comparison operations, ORDER BY clauses, and string functions. PostgreSQL considers distinct collation objects to be incompatible even when they have identical properties, such as the C and POSIX collations.
Typical symptoms
- Comparison operators (<, >, =) fail between columns with different collations
- ORDER BY clauses raise errors when combining columns with conflicting collations
- UNION queries fail when combining result sets with mismatched collations
- Pattern matching operators and string functions like lower(), upper(), and initcap() fail with the error
Common causes
- Columns have conflicting implicit collations (e.g., one column uses de_DE while another uses es_ES)
- Comparing or ordering columns that were created with different default collations
- Attempting to perform string operations on columns without a common collation
- UNION operations combining strings with different collations
How to fix "Indeterminate collation"
First, determine which columns or expressions have conflicting collations. Use this query to check the collations in your table:
SELECT column_name, collation_name
FROM information_schema.columns
WHERE table_name = 'your_table_name'
AND collation_name IS NOT NULL;Also check your database's default collation:
SELECT datcollate
FROM pg_database
WHERE datname = 'your_database_name';Add a COLLATE clause to one of the conflicting expressions to tell PostgreSQL which collation to use. For comparison operations:
SELECT * FROM test1
WHERE column_a < column_b COLLATE "en_US";For ORDER BY:
SELECT * FROM test1
ORDER BY column_a || column_b COLLATE "en_US";When combining queries with UNION, ensure consistent collations by specifying a COLLATE clause:
SELECT name COLLATE "en_US" FROM table1
UNION
SELECT name COLLATE "en_US" FROM table2;To prevent this error in the future, ensure columns that will be compared have the same collation. When creating or altering tables:
CREATE TABLE test1 (
column_a text COLLATE "en_US",
column_b text COLLATE "en_US"
);If you need to change an existing column's collation:
ALTER TABLE test1
ALTER COLUMN column_a TYPE text COLLATE "en_US";Advanced notes
PostgreSQL provides multiple collations including C, POSIX, and locale-specific collations (de_DE, en_US, etc.). The C and POSIX collations are identical and use byte-order comparison. Locale-specific collations follow language-specific rules. When working with databases that support multiple languages or were migrated from systems with different locales, you may need to use explicit COLLATE clauses frequently. In some cases, you can also cast strings to bytea and back to bypass collation requirements, but this is generally not recommended as it loses language-specific sorting semantics.
Frequently asked questions
- What does "Indeterminate collation" mean?
- PostgreSQL uses collations to determine how strings are sorted and compared, including language-specific rules for alphabetical ordering and case sensitivity. When the database encounters conflicting non-default implicit collations among input expressions, it cannot automatically determine which collation rules to apply. This happens most commonly in comparison operations, ORDER BY clauses, and string functions.
- How do I fix "Indeterminate collation"?
- PostgreSQL cannot determine which collation to use when comparing columns with conflicting implicit collations. Resolve by explicitly specifying a collation using the COLLATE clause.
- What causes "Indeterminate collation"?
- Columns have conflicting implicit collations (e.g., one column uses de_DE while another uses es_ES) Comparing or ordering columns that were created with different default collations Attempting to perform string operations on columns without a common collation
- Is the "Indeterminate collation" error serious?
- It is a medium-severity error โ more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- ๐PostgreSQL Official Documentation
- ๐PostgreSQL Collation Support (Current)(Official Docs)
- ๐PostgreSQL Error Codes Appendix(Official Docs)