Skip to main content
0 reactions

How to Drop a Column in PostgreSQL

Featured card for How to Drop a Column in PostgreSQL, with a terminal showing ALTER TABLE members, DROP COLUMN notes, IF EXISTS with no error, and the name and locker columns left.
How to Drop a Column in PostgreSQL

To drop a column in PostgreSQL, use ALTER TABLE … DROP COLUMN with the column’s name. The column and every value in it are gone, and the rest of the table stays. Here’s how, and how to avoid an error when the column may already be gone.

Every command and output on this page is real, from PostgreSQL 19 on macOS.

Syntax

SyntaxSQL
ALTER TABLE table_name
    DROP COLUMN [ IF EXISTS ] column_name [ CASCADE ];
  • table_name: the table the column belongs to.
  • column_name: the column to remove.
  • IF EXISTS: optional. If there’s no such column, skip it with a notice instead of an error.
  • CASCADE: optional. Also drop what depends on the column, such as a view that uses it. Without it, PostgreSQL refuses to drop a column that something else depends on.
  • [ ] marks the optional parts.

See ALTER TABLE (opens in a new tab) in the PostgreSQL documentation.

The Example

A gym keeps its members’ names, locker numbers and a notes column it no longer needs.

Step 1: Create a Test Database

Connect to your server with psql as the postgres user, as Part 2 of the PostgreSQL Tutorial shows. psql connects you to the postgres database, and its prompt is postgres=#.

Create a database for the example and connect to it:

postgres=#SQL
CREATE DATABASE gym;
OutputPlain text
CREATE DATABASE
postgres=#Plain text
\c gym
OutputPlain text
You are now connected to database "gym" as user "postgres".

Step 2: Create the Table and Add a Member

gym=#SQL
CREATE TABLE members (name text, locker integer, notes text);
OutputPlain text
CREATE TABLE
gym=#SQL
INSERT INTO members (name, locker, notes) VALUES ('Lena', 12, 'Prefers mornings');
OutputPlain text
INSERT 0 1
gym=#SQL
SELECT * FROM members;
OutputPlain text
 name | locker |      notes
------+--------+------------------
 Lena |     12 | Prefers mornings
(1 row)

Step 3: Drop the Column

gym=#SQL
ALTER TABLE members DROP COLUMN notes;
OutputPlain text
ALTER TABLE

Here’s the table now:

gym=#SQL
SELECT * FROM members;
OutputPlain text
 name | locker
------+--------
 Lena |     12
(1 row)

The notes column is gone, and Lena’s note with it.

Warning: Dropping a column deletes every value in it, and it can’t be undone.

Step 4: Drop a Column Only If It Exists

Dropping notes again fails, because it’s already gone:

gym=#SQL
ALTER TABLE members DROP COLUMN notes;
OutputPlain text
ERROR:  column "notes" of relation "members" does not exist

IF EXISTS turns the error into a notice, which helps in a script that may run twice:

gym=#SQL
ALTER TABLE members DROP COLUMN IF EXISTS notes;
OutputPlain text
NOTICE:  column "notes" of relation "members" does not exist, skipping
ALTER TABLE

Clean Up

To remove the example, connect to another database and drop the test database:

gym=#Plain text
\c postgres
OutputPlain text
You are now connected to database "postgres" as user "postgres".
postgres=#SQL
DROP DATABASE gym;
OutputPlain text
DROP DATABASE

Warning: DROP DATABASE deletes the database and everything in it, and it can’t be undone.

Common Mistakes

MistakeWhat happensDo this instead
Dropping a column that’s already goneERROR: column "notes" of relation "members" does not existCheck the columns with \d members, or use DROP COLUMN IF EXISTS.
Dropping the wrong columnIts values are gone for good.Check the name twice, and keep a backup of tables that matter.

FAQ

Does dropping a column free disk space?

Not right away. The documentation (opens in a new tab) explains that PostgreSQL makes the column invisible, and its space is reclaimed as the existing rows are updated later.

Conclusion

ALTER TABLE … DROP COLUMN removes a column and all its values. Add IF EXISTS when the column may already be gone, and CASCADE only when you also want what depends on the column removed.

Additional Resources

Postgres, PostgreSQL and the Slonik Logo are trademarks or registered trademarks of the PostgreSQL Community Association of Canada, and used with their permission.

0 comments