PostgreSQL LOWER and UPPER Functions

Need to match emails no matter how people typed them? The PostgreSQL lower function does that in one call. lower('Hannah.Brooks@Gmail.com') returns the text hannah.brooks@gmail.com, and upper() does the reverse.

The catch: WHERE lower(email) = ... skips a plain index on email. My first EXPLAIN showed a Seq Scan over 10,000 rows.

Seven examples follow, from a bare call to indexes and initcap. Tested on PostgreSQL 18.6 for Windows, with psql in VS Code.

PostgreSQL LOWER and UPPER Function Syntax

Here is the syntax:

lower ( text ) → text
upper ( text ) → text
initcap ( text ) → text

The one argument is any string. Other PostgreSQL character types cast to text.

Return value: text, cased by the database’s locale. NULL in, NULL out.

Availability: lower, upper and initcap work in every supported release. casefold() is new in PostgreSQL 18.

When to Use the LOWER Function in PostgreSQL

Use LOWER or UPPER when you need to:

  • find a customer by email whatever the casing
  • block duplicate signups like Hannah@ and hannah@
  • group or filter with LIKE patterns without case surprises

Run this once. Every example uses these six US customers plus 9,994 filler rows:

CREATE EXTENSION IF NOT EXISTS citext;
DROP TABLE IF EXISTS customers;
CREATE TABLE customers (
  id        int PRIMARY KEY,
  full_name text,
  email     text
);
INSERT INTO customers VALUES
 (1, 'hannah brooks',   'Hannah.Brooks@Gmail.com'),
 (2, 'TYLER REED',      'tyler.reed@yahoo.com'),
 (3, 'grace mcdonald',  'GRACE.MCD@Outlook.com'),
 (4, 'marcus o''neil',  'Marcus.ONeil@gmail.com'),
 (5, 'mary-kate ellis', 'MK.Ellis@iCloud.com'),
 (6, 'ÉLODIE DURAND',   NULL);
INSERT INTO customers
SELECT g, 'customer ' || g, 'customer' || g || '@example.com'
FROM generate_series(7, 10000) AS g;
CREATE INDEX customers_email_idx ON customers (email);
ANALYZE customers;

Example 1: Convert Text to Lowercase and Uppercase

Let’s start simple. I want Hannah’s email lowercased and her name in capitals:

\pset null '(null)'

SELECT id, lower(email) AS email_lc, upper(full_name) AS name_uc
FROM customers
WHERE id IN (1, 6);

Output:

 id |        email_lc         |    name_uc    
----+-------------------------+---------------
  1 | hannah.brooks@gmail.com | HANNAH BROOKS
  6 | (null)                  | ÉLODIE DURAND
(2 rows)

For id 1, this returns hannah.brooks@gmail.com and HANNAH BROOKS. Élodie’s NULL email stays NULL. To join names first, use CONCAT.

Example 2: Case-Insensitive Search in PostgreSQL (lower vs ILIKE vs citext)

Now the search. The goal is to find hannah.brooks@gmail.com four ways:

SELECT count(*) FILTER (WHERE email = k)         AS plain,
       count(*) FILTER (WHERE lower(email) = k)  AS lower,
       count(*) FILTER (WHERE email ILIKE k)     AS ilike,
       count(*) FILTER (WHERE email::citext = k::citext) AS citext
FROM customers, (VALUES ('hannah.brooks@gmail.com')) AS v(k);

Output:

 plain | lower | ilike | citext 
-------+-------+-------+--------
     0 |     1 |     1 |      1
(1 row)
PostgreSQL case-insensitive search: plain = returns 0, lower(), ILIKE and citext each return 1
Only plain = misses the row.

Plain = gives 0; the other three give 1. Careful: in ILIKE, _ and % are wildcards.

Example 3: Speed Up lower() with an Expression Index

Now the intro’s catch. Say you need Tyler’s row fast:

\t on
EXPLAIN (COSTS OFF)
SELECT id FROM customers WHERE lower(email) = 'tyler.reed@yahoo.com';

CREATE INDEX customers_lower_email_idx ON customers (lower(email));
ANALYZE customers;

EXPLAIN (COSTS OFF)
SELECT id FROM customers WHERE lower(email) = 'tyler.reed@yahoo.com';

Output:

 Seq Scan on customers
   Filter: (lower(email) = 'tyler.reed@yahoo.com'::text)

 Index Scan using customers_lower_email_idx on customers
   Index Cond: (lower(email) = 'tyler.reed@yahoo.com'::text)
PostgreSQL EXPLAIN showing Seq Scan for lower(email) before and Index Scan using customers_lower_email_idx after
Seq Scan before, Index Scan after.

The first plan shows a Seq Scan, the second an Index Scan. Your WHERE must repeat lower(email) exactly. I have a separate tutorial on PostgreSQL CREATE INDEX.

Example 4: Enforce Unique Case-Insensitive Emails

I want PostgreSQL to reject a second, lowercase Hannah:

\set ON_ERROR_STOP off

CREATE UNIQUE INDEX customers_email_ci_uq ON customers (lower(email));

INSERT INTO customers
VALUES (10001, 'Hannah Brooks', 'hannah.brooks@gmail.com');

Output:

psql:lower-unique-email-index.sql:6: ERROR:  duplicate key value violates unique constraint "customers_email_ci_uq"
DETAIL:  Key (lower(email))=(hannah.brooks@gmail.com) already exists.
PostgreSQL unique index on lower(email) rejecting hannah.brooks@gmail.com with duplicate key error
The case variant is rejected.

The INSERT fails, and the DETAIL line shows the lower(email) key. A UNIQUE constraint can’t hold an expression, so use a unique index.

Example 5: Capitalize US Names with initcap

The names are a mess. The goal is title case:

SELECT full_name, initcap(full_name) AS fixed
FROM customers
WHERE id IN (3, 4, 5)
UNION ALL
SELECT 'pat''s diner', initcap('pat''s diner')
ORDER BY 2;

Output:

    full_name    |      fixed      
-----------------+-----------------
 grace mcdonald  | Grace Mcdonald
 marcus o'neil   | Marcus O'Neil
 mary-kate ellis | Mary-Kate Ellis
 pat's diner     | Pat'S Diner
(4 rows)
PostgreSQL initcap returning Grace Mcdonald, Marcus O'Neil and Pat'S Diner
Mcdonald and Pat’S are wrong.

Marcus O’Neil comes back right. Mcdonald and Pat’S don’t: initcap starts a word after any non-letter. Fix them with REPLACE.

Example 6: Lowercase Accented Letters (Collation)

Case rules follow the locale. I want to test É:

SELECT lower('ÉLODIE')             AS en_us,
       lower('ÉLODIE' COLLATE "C") AS c_collation;

Output:

 en_us  | c_collation 
--------+-------------
 élodie | Élodie
(1 row)

In en-US, ÉLODIE gives élodie. With COLLATE “C” it gives Élodie, because C only maps A-Z.

Example 7: Convert a Column to Lowercase with UPDATE

Last, a cleanup. Say you want stored emails lowercased and names title-cased:

\pset null '(null)'

UPDATE customers
SET email     = lower(email),
    full_name = initcap(full_name)
WHERE id <= 6;

SELECT full_name, email FROM customers WHERE id = 3;

Output:

   full_name    |         email         
----------------+-----------------------
 Grace Mcdonald | grace.mcd@outlook.com
(1 row)

The check query shows grace.mcd@outlook.com. Run the SELECT again and you should see it stored. More in my PostgreSQL UPDATE walkthrough.

Tips & Common Mistakes

  • A plain index on email never serves lower(email).
  • lower(NULL) is NULL, so lower(email) = '' never finds missing emails.
  • Without the extension, casts fail with type "citext" does not exist.
  • Cast both sides to citext. Comparing email::citext to a text value gave me 0, a case-sensitive match.

Wrapping Up

The PostgreSQL lower function plus an expression index covers most case-insensitive lookups. Copy Example 3 for search and Example 4 for signups.

Next step: browse the complete list of PostgreSQL functions or the PostgreSQL 18 string functions docs.

More PostgreSQL string tutorials:

Frequently Asked Questions

Is PostgreSQL case sensitive?

String comparisons are. Unquoted identifiers fold to lowercase, so Customers means customers.

Is ILIKE slower than lower()?

Both scan without an index. ILIKE needs a pg_trgm GIN index to speed up.

How do I make a column case-insensitive in PostgreSQL?

Use citext, or a nondeterministic ICU collation (PostgreSQL 12+).