TEXT data type but treats values as case-insensitive when comparing or indexing, making it useful for case-insensitive searches and unique constraints.
Your Nile database arrives with citext extension already enabled, so there’s no need to run create extension.
Creating and Populating a Sample Table
Before creating the index, let’s create a sample table and populate it with data:Benefits of Using citext
- Allows case-insensitive text comparisons without using
LOWER(). - Simplifies case-insensitive unique constraints and indexes.
- Reduces errors when working with user-provided text data like emails or usernames.
Creating an Index on citext Columns
A B-tree index can be created on aCITEXT column just like a TEXT column:
Enforcing Unique Constraints with citext
AUNIQUE constraint on a CITEXT column ensures case-insensitive uniqueness:
'APPLE' or 'apple' would result in a constraint violation.
Querying with citext
Once thecitext extension is enabled, queries automatically become case-insensitive:
'Apple', 'APPLE', or 'apple'.
Limitations
CITEXTis slightly slower thanTEXTdue to case normalization.- It does not support LIKE queries efficiently unless you create a functional index using
LOWER(column1). - Collation-sensitive operations may not always behave as expected.
Removing a citext Index
If you need to remove an index:Conclusion
Thecitext extension in PostgreSQL simplifies case-insensitive text handling, making it ideal for usernames, emails, and other text fields where case differences should be ignored.
For more details, refer to the PostgreSQL documentation.