PostgreSQL: Script to Drop Foreign Keys

PostgreSQL: Script to Drop Foreign Keys

novicetechie

Sometimes you may need to drop foreign key constraints while restructuring tables. PostgreSQL allows you to do this easily.

Drop a Specific Foreign Key


ALTER TABLE orders
DROP CONSTRAINT orders_customer_id_fkey;

Drop All Foreign Keys in a Schema


SELECT 'ALTER TABLE ' || tc.table_name ||
       ' DROP CONSTRAINT ' || tc.constraint_name || ';'
FROM information_schema.table_constraints tc
WHERE tc.constraint_type = 'FOREIGN KEY'
AND tc.table_schema = 'public';

Run the generated statements to remove all foreign keys.

Comments

Simple Tech Tips for Everyday Users

Learn easy tech solutions, beginner-friendly tutorials, and simple fixes for your phone, laptop, apps, and internet problems — all in one place.

Explore by Category

About NoviceTechie

NoviceTechie.com is your simple tech helper. We provide beginner-friendly tutorials, how-to guides, and easy troubleshooting tips to make technology easier for everyone — no technical knowledge required.

Latest Posts

Get Simple Tech Tips Weekly

Join our free updates (optional).

Follow Us
NoviceTechie.com — Simple tech tutorials, everyday problem fixes, and easy guides for beginners.