Add unique constraint to an existing PostgreSQL index Add unique constraint to an existing PostgreSQL index sql sql

Add unique constraint to an existing PostgreSQL index


There is no way to change an index to unique index. You can see what you can change to index at alter index document.

In this case you need to drop and create new unique index.


You can alter an index to add the "UNIQUE" constraint with:

 ALTER TABLE distributors  ADD CONSTRAINT dist_id_zipcode_key UNIQUE (dist_id, zipcode);

See the official documentation here: https://www.postgresql.org/docs/current/sql-altertable.html