The citext Extension
Use the citext extension to handle case-insensitive data in Postgres
The citext
extension in Postgres provides a case-insensitive data type for text. This is particularly useful in scenarios where the case of text data should not affect queries, such as usernames or email addresses, or any form of textual data where case-insensitivity is desired.
This guide covers the citext
extension — its setup, usage, and practical examples in Postgres. For datasets where consistent text formatting isn't guaranteed, case-insensitive queries can streamline operations.
note
The citext
extension is an open-source module for Postgres. It can be easily installed and used in any Postgres database. This guide provides steps for installation and usage, with further details available in the Postgres Documentation.
citext
extension
Enable the You can enable citext
by running the following CREATE EXTENSION
statement in the Neon SQL Editor or from a client such as psql
that is connected to Neon.
For information about using the Neon SQL Editor, see Query with Neon's SQL Editor. For information about using the psql
client with Neon, see Connect with psql.
Example usage
Creating a table with citext
Consider a user registration system where the user's email should be unique, regardless of case.
In this table, the email
field is of type citext
, ensuring that email addresses are treated case-insensitively.
Inserting data
Insert data as you would normally. The citext
type automatically handles case-insensitivity.
Case-insensitive querying
Queries against citext
columns are inherently case-insensitive. Effectively, it calls the lower()
function on both strings when comparing two values.
This query returns the following:
The email address matched even though the case was different.
More examples
Using citext with regex functions
The citext
extension can be used with regular expressions and other string-matching functions, which perform string matching in a case-insensitive manner.
For example, the query below finds users whose email addresses start with 'AL'.
This query returns the following:
Using citext data as TEXT
If you do want case-sensitive behavior, you can cast citext
data to text
and use it as shown here:
Query:
This query will only return results if it finds a user with an email address containing 'EVE'.
Benefits of Using citext
- Query simplicity: No need for functions like
lower()
orupper()
to perform case-insensitive comparisons. - Data integrity: Helps maintain data consistency, especially in user input scenarios.
Performance considerations
Indexing with citext
Indexing citext
fields is similar to indexing regular text fields. However, it's important to note that the index will be case-insensitive.
This index will improve the performance of queries involving the email
field. Depending on whether the more frequent use case is case-sensitive or case-insensitive, you can choose to index the citext
field or cast it to text
and index that.
lower()
function
Comparison with Citext
internally does an operation similar to lower()
on both sides of the comparison, so there is not a big performance jump. However, using citext
ensures consistent case-insensitive behavior across queries without the need for repeatedly applying the lower()
function, which makes errors less likely.
Conclusion
The citext
extension helps manage case-insensitivity in text data within Postgres. It simplifies queries and ensures consistency in data handling. This guide provides an overview of using citext
, including creating and querying case-insensitive fields.
Resources
Need help?
Join our Discord Server to ask questions or see what others are doing with Neon. Users on paid plans can open a support ticket from the console. For more detail, see Getting Support.
Last updated on