Is it possible in PostgreSQL to create a deferrable unique constraint on a character column, but case-insensitive?
Let\'s assume the following basic table:
You can circumvent the restriction by using the special type citext provided by the additional module of the same name. Quoting the manual:
The
citext
module provides a case-insensitive character string type, citext. Essentially, it internally calls lower when comparing values. Otherwise, it behaves almost exactly liketext
.
It addresses your case exactly. Run once per database:
CREATE EXTENSION citext;
Then you can:
CREATE TABLE sample_table (
my_column citext
,CONSTRAINT my_unique_constraint UNIQUE(my_column)
DEFERRABLE INITIALLY IMMEDIATE
);