Allxie
Allxie

Reputation: 573

Add partial uniqueness constraint in postgres on a row such that one value is okay to duplicate

I want to create a table of emails emails where all values in the "email" column must be unique except one for a "[email protected]" placeholder. Example:

I see in postgres documentation that it's straightforward to allow null values to be non-duplicative, but is there a way to do this with another value?

Upvotes: 1

Views: 49

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270773

You can create a filtered unique index:

create unique index unq_emails_email on (email)
    where email <> '[email protected]';

Upvotes: 2

Related Questions