Common SQL Errors

PostgreSQL collation_mismatch Error (42P21) – Causes and Fixes

August 4, 2025

The collation_mismatch error (42P21) fires when PostgreSQL evaluates text operands with incompatible collations in the same expression or query clause.

Sign up for the latest in common SQL errors from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

What is PostgreSQL error 42P21 collation_mismatch?

PostgreSQL error 42P21 (collation_mismatch) happens when a query mixes text values that use different collations. Explicitly coerce every text value to the same collation with the COLLATE clause or change the column definitions to share one collation to resolve the error.

Error Highlights

Typical Error Message

PostgreSQL Error 42P21

Error Type

Collation Error

Language

PostgreSQL

Symbol

collation_mismatch

Error Code

42P21

SQL State

Explanation

Table of Contents

What is PostgreSQL error 42P21 (collation_mismatch)?

Error 42P21 tells PostgreSQL that two or more text operands in the same expression carry different collations. PostgreSQL must apply one set of collation rules to compare, sort, or aggregate text. Mixed rules leave the planner unable to decide which to use, so it stops with collation_mismatch.

The error surfaces while comparing text columns, ordering results, grouping, or using text functions where implicit collations are inherited from column definitions or database defaults.

Fixing it is important because it blocks query execution and can hide deeper data-quality issues.

What Causes This Error?

Implicit collations on text columns often differ across schemas or after migrations. When queries join or compare these columns, PostgreSQL inherits incompatible collations and raises 42P21.

Literal strings adopt the database default collation.

Mixing a literal with a differently collated column triggers the mismatch unless the literal is cast with COLLATE.

How to Fix PostgreSQL Error 42P21

Force every operand to share one collation. Use the COLLATE clause inline, cast literals, or redefine columns to the same collation. Verify the server’s default collation if literals are involved.

For transient fixes, add COLLATE "en_US" (or your target) to each operand.

For long-term stability, align column collations during schema design and migrations.

Common Scenarios and Solutions

JOIN or WHERE clause comparing name columns from two tables: apply COLLATE to one side or alter the column definition.

ORDER BY on a view that selects mixed-collation columns: adjust the SELECT list with COLLATE.

Best Practices to Avoid This Error

Standardize one collation per database or at least per logical data domain.

Document it in migration files.

Use explicit COLLATE in code whenever a query crosses schemas or databases. Automated linters in tools like Galaxy can flag unqualified text comparisons.

Related Errors and Solutions

42P22 (indeterminate collation): occurs when the result collation cannot be deduced. Apply explicit COLLATE.

42804 (datatype_mismatch): arises when operands have incompatible data types. Cast explicitly.

0A000 (feature_not_supported) for ICU collations on older versions: upgrade or switch to ICU-enabled build.

.

Common Causes

Related Errors

FAQs

Why did this error appear after a migration?

The migration likely created new tables with the server’s current collation, differing from existing tables. Mixing old and new columns then produced 42P21.

Can I disable collation checks?

No safe setting turns them off. For correctness, PostgreSQL must enforce one collation per comparison. Always fix the query or schema.

Is COLLATE a performance risk?

Explicit COLLATE adds negligible overhead. Indexes on the same collation remain usable, so queries stay fast.

How does Galaxy help?

Galaxy’s editor detects collation mismatches during linting and suggests the proper COLLATE clause, reducing runtime errors.

Start Querying with the Modern SQL Editor Today!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Check out some other errors

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo