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
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:
CREATE DATABASE gym;
CREATE DATABASE
\c gym
You are now connected to database "gym" as user "postgres".
Step 2: Create the Table and Add a Member
CREATE TABLE members (name text, locker integer, notes text);
CREATE TABLE
INSERT INTO members (name, locker, notes) VALUES ('Lena', 12, 'Prefers mornings');
INSERT 0 1
SELECT * FROM members;
name | locker | notes
------+--------+------------------
Lena | 12 | Prefers mornings
(1 row)
Step 3: Drop the Column
ALTER TABLE members DROP COLUMN notes;
ALTER TABLE
Here’s the table now:
SELECT * FROM members;
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:
ALTER TABLE members DROP COLUMN notes;
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:
ALTER TABLE members DROP COLUMN IF EXISTS notes;
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:
\c postgres
You are now connected to database "postgres" as user "postgres".
DROP DATABASE gym;
DROP DATABASE
Warning: DROP DATABASE deletes the database and everything in it, and it can’t be undone.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Dropping a column that’s already gone | ERROR: column "notes" of relation "members" does not exist | Check the columns with \d members, or use DROP COLUMN IF EXISTS. |
| Dropping the wrong column | Its 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
- PostgreSQL 19 documentation: ALTER TABLE (opens in a new tab)
- PostgreSQL 19 documentation: Removing a Column (opens in a new tab)
Related
- PostgreSQL Tutorial — Part 6: Create Tables
- How to Add a Column to a Table in PostgreSQL
- How to Rename a Column in PostgreSQL
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