How to Drop a Column in PostgreSQL

Galaxy Glossary

How do I safely drop a column in PostgreSQL?

Removes an existing column from a table, permanently deleting its data and metadata.

Sign up for the latest in SQL knowledge from the Galaxy Team!

Description

Why would you drop a column?

Free disk space, simplify the schema, or remove sensitive data no longer required. Dropping eliminates the column and its contents forever, so create a backup first.

What is the basic DROP COLUMN syntax?

Use ALTER TABLE followed by the table name, then DROP COLUMN and the column name. Add IF EXISTS to avoid errors when the column might already be gone.

Syntax breakdown

ALTER TABLE instructs PostgreSQL to change a table; DROP COLUMN removes the specified column; CASCADE drops dependent objects like indexes or views, while RESTRICT (default) blocks the action if dependencies exist.

How do you safely remove a column?

1️⃣ Validate no application code or queries reference the column. 2️⃣ Check dependencies with pg_depend or psql \d+. 3️⃣ Issue ALTER TABLE … DROP COLUMN inside a transaction so you can ROLLBACK if needed.

Step-by-step example

BEGIN; ALTER TABLE "Orders" DROP COLUMN shipping_method; COMMIT; The transaction ensures either full success or no change.

Does DROP COLUMN lock the table?

Yes, PostgreSQL takes an ACCESS EXCLUSIVE lock for a very short time. Reads and writes wait during the catalog update, but the lock is usually milliseconds unless the table is huge.

Can you drop multiple columns at once?

Yes. Separate each DROP COLUMN clause with a comma: ALTER TABLE "Products" DROP COLUMN supplier_id, DROP COLUMN discontinued_at;

Best practices

Back up first, drop in low-traffic windows, monitor replication lag, test in staging, and always use IF EXISTS in automated scripts.

Common mistakes to avoid

Using CASCADE blindly can delete views or foreign keys you still need. Forgetting to update ORM models causes runtime errors after deployment.

Why How to Drop a Column in PostgreSQL is important

How to Drop a Column in PostgreSQL Example Usage


-- Remove the obsolete discount column from OrderItems
aALTER TABLE "OrderItems"
    DROP COLUMN IF EXISTS discount CASCADE;

How to Drop a Column in PostgreSQL Syntax


ALTER TABLE [IF EXISTS] table_name
    DROP COLUMN [IF EXISTS] column_name
    [CASCADE | RESTRICT];

-- Drop several columns
ALTER TABLE table_name
    DROP COLUMN column_a,
    DROP COLUMN column_b;

-- Ecommerce example
a-- Remove a deprecated promotion_code column from Orders
aALTER TABLE "Orders" DROP COLUMN IF EXISTS promotion_code;

Common Mistakes

Frequently Asked Questions (FAQs)

Is dropping a column reversible?

No. You must restore from backup or recreate the column and reload data.

Does it affect table performance?

After dropping, sequential scans become slightly faster because fewer columns are read from disk.

Can I drop a column used in a UNIQUE constraint?

Only after removing or altering the constraint; otherwise PostgreSQL blocks the operation.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
BauHealth Logo
Truvideo Logo