Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add case insensitive collation to PostgreSQL

By default, PostgreSQL comes without case insensitive collations. Its documentation describes a way to add new collations based on OS locales, e.g.:

http://www.postgresql.org/docs/9.1/static/sql-createcollation.html

however the created collations are case sensitive.

My question is: How to add a case INsensitive collation to a PostgreSQL server ?

Can somebody describe a workflow ?

I was not able to find any references on the net, while some people mention that it is possible. I've tested with v9.1 - Debian GNU/Linux and v9.2 on Windows.

Just to clarify that I'm aware of:

  • PostgreSQL CITEXT add-on data type
  • Usage of LOWER/UPPER for searches and indexes.

These are not the solutions that I want.

like image 471
Delian Krustev Avatar asked Jul 02 '13 09:07

Delian Krustev


People also ask

How do I make a case-insensitive in PostgreSQL?

While using regular expressions, we need to use the PostgreSQL ~* operator instead of the like operator; we can also use the ilike operator in PostgreSQL. We can also create an extension name as citext to use the case insensitive query in PostgreSQL; we need to create it first to use the extension of citext.

Which collation is case-insensitive?

A case-insensitive collation ignores the differences between uppercase and lowercase letters for string comparison and sorting, whereas a case-sensitive collation does not. For example, in case-insensitive collation, “A” and “a” are equal.

Is Postgres order by case-insensitive?

No, these both are the same, just a different naming convention. As a_horse_with_no_name said, Postgres uses the collation implementation from the OS. There is no way to get the same result on both operating systems. In your case you may(I said maybe)do like this: ORDER BY lower(fieldname) .

Is like case sensitive in PostgreSQL?

String comparisons in PostgreSQL are case sensitive* (unless a case-insensitive collation were to be introduced). To work around this, PostgreSQL has several methods to match strings in a case-insensitive manner. Consequently, Marten also makes use of these methods to translate case-insensitive queries to pgSQL.


2 Answers

"Nondeterministic collations" were added to Version 12:

"The most typical use case for nondeterministic collations is probably the case-insensitive comparison. At secondary strength, strings that differ by case compare as equal"

like image 166
Doug Winsby Avatar answered Sep 30 '22 09:09

Doug Winsby


Doesn't seem to be possible within Postgres iteself:

Not from the Postgres project -- we just use the collations supplied by the operating system.

like image 35
DrColossos Avatar answered Sep 30 '22 08:09

DrColossos