Removing (dropping) a Foreign Key Constraint in PostgreSQL
Had a need to drop a Foreign Key Constraint in PostgreSQL 8.x today, and this is how you do it:
database=> \d table_name;
Table "public.table_name"
Column | Type | Modifiers
------------------+------------------------+-----------
id | integer |
field | character varying(20) |
field_description | character varying(150) |
Indexes:
[..]
Foreign-key constraints:
"table_name_id_fkey" FOREIGN KEY (id) REFERENCES other_table(id) ON DELETE CASCADE
database=> ALTER TABLE table_name DROP CONSTRAINT "table_name_id_fkey";
ALTER TABLE
database=>
As simple as that. The name of the constraint is shown when describing the table with \d under “Foreign-key constraints”, and you simply do an ALTER statement to drop the constraint.
Tags: constraints, Databases, foreign keys, postgres, postgresql

February 9th, 2011 at 01:40
Thanks Mats! I searched for this on Google and your page popped up. I didn’t see your name at first, but it somehow appeared on my retina after I had left the page so I had to go back and check.
Hope you are doing good!
And, yes, the constraint is gone.
–
Lars, currently in Palo Alto.