PostgreSQL - How to add constraint to an existing table?
You have a table already created with lots of data in PostgreSQL, now you want to add a constraint to that table. Use the ALTER TABLE
add a constraint.
Here is the generic syntax to add a constraint:
ALTER TABLE table_name
ADD CONSTRAINT constraint_name constraint_definition;
For example, to add a UNIQUE constraint to the table customers on the column customer_id:
ALTER TABLE customers
ADD CONSTRAINT uniquectm_const UNIQUE (customer_id);
Need a good GUI tool for PostgreSQL? Check out TablePlus. It’s native, beautiful, and available for free.
Not on Mac? Download TablePlus for Windows.
On Linux? Download TablePlus for Linux
Need a quick edit on the go? Download TablePlus for iOS