Constraint name update in PostgreSQL

Sebastian picture Sebastian · Jun 9, 2009 · Viewed 44.3k times · Source

Is it possible to change the constraint name in Postgres? I have a PK added with:

ALTER TABLE contractor_contractor ADD CONSTRAINT commerce_contractor_pkey PRIMARY KEY(id);

And I want to to have different name for it, to be consistent with the rest of the system. Shall I delete the existing PK constraint and create a new one? Or is there a 'soft' way to manage it?

Thanks!

Answer

Arturo Herrero picture Arturo Herrero · Aug 7, 2014

To rename an existing constraint in PostgreSQL 9.2 or newer, you can use ALTER TABLE:

ALTER TABLE name RENAME CONSTRAINT constraint_name TO new_constraint_name;