537

Is there any way to write case-insensitive queries in PostgreSQL, E.g. I want that following 3 queries return same result.

SELECT id FROM groups where name='administrator'

SELECT id FROM groups where name='ADMINISTRATOR'

SELECT id FROM groups where name='Administrator'
3
  • 1
    if citext comes with your Postgres installation, try citext type. It's case-insensitive text Aug 10, 2011 at 4:06
  • 4
    For newcomers to this question, this link to official postgres documentation contains all the answers given here, as well as a few other options. Jul 7, 2014 at 20:38
  • 2
    Sir reassign accepted answer to the one made by @Arun please. It is much less complicated and do not pull bunch of troubles after apply.
    – zelibobla
    Jun 7, 2016 at 17:25

14 Answers 14

667

Use LOWER function to convert the strings to lower case before comparing.

Try this:

SELECT id 
  FROM groups
 WHERE LOWER(name)=LOWER('Administrator')
15
  • 139
    It's important to note that using LOWER (or any function) on the predicate columns--in this case "name"--will cause any indexes to no longer be seekable. If this is a large or frequently queried table, that could cause trouble. Case-insensitive collation, citext, or a function-based index will improve performance.
    – Jordan
    Aug 10, 2011 at 4:23
  • 144
    Or just create an index like this: CREATE INDEX idx_groups_name ON groups lower(name);
    – Daniel
    Aug 10, 2011 at 5:25
  • 24
    Also specify varchar_pattern_ops if you want the index to work with LIKE 'xxx%' query, i.e. CREATE INDEX ix_groups_name ON groups (lower(name) varchar_pattern_ops).
    – sayap
    Aug 10, 2011 at 11:44
  • 21
    Using the ILIKE operator (as shown in other answers below) is a simpler approach, even though this is the most voted answer.
    – Ryan
    Apr 21, 2015 at 19:36
  • 9
    Going through the comments here, a lot of suggestions here suggests ILIKE, It will work, but with slow response. To obtain fast access to tables based on the results of computations, I suggest anyone just checking this should go with the accepted answer. See more details here and here Dec 1, 2016 at 19:35
500

using ILIKE instead of LIKE

SELECT id FROM groups WHERE name ILIKE 'Administrator'
5
  • 8
    Note that ILIKE is not supported by Hibernate when used in Spring Boot.
    – AnT
    Mar 27, 2020 at 2:00
  • 2
    @AnT it works with org.hibernate.dialect.PostgreSQL94Dialect and Spring Boot 2.0.6.RELEASE. But IntelliJ complains about it.
    – skaveesh
    May 2, 2020 at 7:46
  • 1
    Is ilike going to be much slower? Especially when the field is indexed?
    – Ye Zhang
    Jun 8, 2022 at 20:19
  • 2
    This is the actual answer, the accepted one is a hack Aug 26, 2022 at 8:58
  • 2
    Actually, ILIKE is the simplest answer but not the "actual" answer for all cases. ILIKE is a non-standard extension to Postgres and it will perform very slowly. The accepted answer using the LOWER function, along with proper indexing, will perform much better and will be supported by all client libraries and ORMS. It depends on your particular use case.
    – gerrard00
    Dec 5, 2022 at 15:20
163

The most common approach is to either lowercase or uppercase the search string and the data. But there are two problems with that.

  1. It works in English, but not in all languages. (Maybe not even in most languages.) Not every lowercase letter has a corresponding uppercase letter; not every uppercase letter has a corresponding lowercase letter.
  2. Using functions like lower() and upper() will give you a sequential scan. It can't use indexes. On my test system, using lower() takes about 2000 times longer than a query that can use an index. (Test data has a little over 100k rows.)

There are at least three less frequently used solutions that might be more effective.

  1. Use the citext module, which mostly mimics the behavior of a case-insensitive data type. Having loaded that module, you can create a case-insensitive index by CREATE INDEX ON groups (name::citext);. (But see below.)
  2. Use a case-insensitive collation. This is set when you initialize a database. Using a case-insensitive collation means you can accept just about any format from client code, and you'll still return useful results. (It also means you can't do case-sensitive queries. Duh.)
  3. Create a functional index. Create a lowercase index by using CREATE INDEX ON groups (LOWER(name));. Having done that, you can take advantage of the index with queries like SELECT id FROM groups WHERE LOWER(name) = LOWER('ADMINISTRATOR');, or SELECT id FROM groups WHERE LOWER(name) = 'administrator'; You have to remember to use LOWER(), though.

The citext module doesn't provide a true case-insensitive data type. Instead, it behaves as if each string were lowercased. That is, it behaves as if you had called lower() on each string, as in number 3 above. The advantage is that programmers don't have to remember to lowercase strings. But you need to read the sections "String Comparison Behavior" and "Limitations" in the docs before you decide to use citext.

18
  • 1
    About #1: It shouldn't be a problem, since it would be two different strings (think of it like doing col = 'a' and col = 'b'). About #2: As you said, you can create an index on an expression, so it's not really a problem. But I agree with you that changing the collation is most likely the best solution. Aug 10, 2011 at 4:21
  • 5
    Can someone tell me what case-insensitive collations are PostgreSQL built-in collations ? I see this as an option but can't find anything about a case-insensitive collation for Postgres on the net ?
    – khorvat
    Dec 20, 2013 at 13:40
  • 1
    @AnupShah: No, I'm not saying that. I'm not running PostgreSQL on Windows. The 9.4 docs say this: "On all platforms, the collations named default, C, and POSIX are available. Additional collations may be available depending on operating system support." You can see which collations PostgreSQL thinks are available with select * from pg_collation;. May 12, 2015 at 22:18
  • 1
    @Matthieu: This is best introduction (and caution) to the subject that I know about: Edge Cases to Keep in Mind. Part 1 – Text. Aug 10, 2017 at 16:08
  • 1
    @Matthieu: The Unicode FAQ is also fun to read. Here's Why's there no unique uppercase character for . . . Aug 10, 2017 at 16:15
132

You can use ILIKE. i.e.

SELECT id FROM groups where name ILIKE 'administrator'
3
  • Its correct and working fine for me, I am using MAC OS X(Mountain Lion).
    – ADJ
    Apr 4, 2013 at 5:58
  • 9
    This will work, but with slow response. To obtain fast access to tables based on the results of computations, I suggest to use the lower function. See more details Dec 1, 2016 at 19:28
  • 3
    @AfolabiOlaoluwaAkinwumi fundamentally this comes down to whether you're searching for results opposed to filtering known values. In the latter case, a single uniform case should be persisted at the data level allowing the equality operator to work. [Personal recommendation is upper pascal case for type code values] May 1, 2017 at 20:10
58

You can also read up on the ILIKE keyword. It can be quite useful at times, albeit it does not conform to the SQL standard. See here for more information: http://www.postgresql.org/docs/9.2/static/functions-matching.html

8
  • 11
    Something to watch out for here is malicious user input. If you run a query like email ILIKE 'user-input-email-here', make sure to escape the user input. Otherwise people can enter characters like % that match anything. May 13, 2013 at 19:23
  • 2
    @MattDeLeon Hi. Well said. But I just want to ask you, if I use ILIKE and prepared statements will this protect me from sql injection?
    – slevin
    Oct 9, 2013 at 0:00
  • 1
    "The key word ILIKE can be used instead of LIKE to make the match case-insensitive according to the active locale. This is not in the SQL standard but is a PostgreSQL extension." Works like a charm in 9.3 Dec 23, 2014 at 20:38
  • 2
    ILIKE is slower than lower(column_name) like %expression%. Oct 3, 2017 at 11:53
  • 1
    @PatrykImosa: Can you please elaborate or show an example of ILIKE being slower?
    – Blama
    Dec 3, 2019 at 16:48
36

You could also use POSIX regular expressions, like

SELECT id FROM groups where name ~* 'administrator'

SELECT 'asd' ~* 'AsD' returns t

5
  • 1
    I had the same problem, I needed case insensitive searches on my PostgreSQL database. I thought about transforming the user input string into a regular expression. Now, using ~* instead of = or LIKE worked perfectly! I didn't need to create new indexes, columns or whatever. Sure, regex search is slower than straight byte comparison, but I don't think the impact on performance would be so much greater than having to handle two sets of data (one lower or uppercased just for searching, then having to retrieve the corresponding original data from the other set). Besides, this is cleaner! Jan 11, 2015 at 3:21
  • 1
    Fine, but how to do with regexp_matches() for example ?
    – WKT
    Sep 9, 2016 at 17:42
  • 2
    According to postgres docs: The operator ~~ is equivalent to LIKE, and ~~* corresponds to ILIKE. There are also !~~ and !~~* operators that represent NOT LIKE and NOT ILIKE, respectively. All of these operators are PostgreSQL-specific.
    – sh4
    Aug 30, 2019 at 13:02
  • 1
    I faced a issue when brackets are included in the text, its not working. like: "code (LC)" Sep 23, 2019 at 16:06
  • faced issues with special characters.
    – Ben Walton
    Jan 10, 2022 at 19:18
27

use ILIKE

select id from groups where name ILIKE 'adminstration';

If your coming the expressjs background and name is a variable use

select id from groups where name ILIKE $1;
2
  • what's the performance tradeoff between direct matching and ILIKE ?
    – Nikhil VJ
    Aug 2, 2022 at 6:54
  • The idea is to make sure not you match every string variations, To get to your point, The time and space are the same, However ILIKE does a deep comparision
    – MUGABA
    Aug 2, 2022 at 7:13
14

Using ~* can improve greatly on performance, with functionality of INSTR.

SELECT id FROM groups WHERE name ~* 'adm'

return rows with name that contains OR equals to 'adm'.

2
  • 4
    Hey, Robin, welcome to SO. James Brown's answer already proposed this solution. Additionally, your proposed answer does not leverage regex in any way.
    – Rafael
    Oct 8, 2018 at 16:42
  • 1
    ~* is not precisely case-insensitive match. It is regex pattern matching. In your example if db contains ADM then where name ~* 'Adm' or where name ~* 'Ad' would yield results. Use ILIKE instead
    – nabster
    Oct 14, 2021 at 21:32
10

ILIKE work in this case:

SELECT id 
  FROM groups
 WHERE name ILIKE 'Administrator'
0
2

For a case-insensitive parameterized query, you can use the following syntax:

 "select * from article where upper(content) LIKE upper('%' || $1 || '%')"
1
-- Install 'Case Ignore Test Extension'
create extension citext;

-- Make a request
select 'Thomas'::citext in ('thomas', 'tiago');

select name from users where name::citext in ('thomas', 'tiago');
1

If you want not only upper/lower case but also diacritics, you can implement your own func:

CREATE EXTENSION unaccent;

CREATE OR REPLACE FUNCTION lower_unaccent(input text)
 RETURNS text
 LANGUAGE plpgsql
AS $function$
BEGIN
    return lower(unaccent(input));
END;
$function$;

Call is then

select lower_unaccent('Hôtel')
>> 'hotel'
1

A tested approach is using ~*

As in the example below

SELECT id FROM groups WHERE name ~* 'administrator'
-18
select id from groups where name in ('administrator', 'ADMINISTRATOR', 'Administrator')
1
  • Please add some explanation to your answer such that others can learn from it
    – Nico Haase
    Oct 22, 2021 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.