To drop a table in PostgreSQL, use DROP TABLE with the table’s name. It deletes the table and every row in it. Here’s how, how IF EXISTS avoids an error, and what CASCADE does when another table depends on the one you drop.
Every command and output on this page is real, from PostgreSQL 19 on macOS.
Syntax
DROP TABLE [ IF EXISTS ] table_name [, ...] [ CASCADE | RESTRICT ];
table_name: the table to delete. Separate several names with commas to drop them together.IF EXISTS: optional. If there’s no such table, skip it with a notice instead of an error.CASCADE: optional. Also drop what depends on the table, such as a foreign key in another table or a view that uses it.RESTRICT: refuse to drop the table when anything depends on it. This is what happens when you give neither.[ ]marks the optional parts, and|separates choices.
See DROP TABLE (opens in a new tab) in the PostgreSQL documentation.
The Example
A small league has a notes table it no longer needs, plus teams and the players in each team.
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 league;
CREATE DATABASE
\c league
You are now connected to database "league" as user "postgres".
Step 2: Drop a Table
CREATE TABLE notes (body text);
CREATE TABLE
DROP TABLE notes;
DROP TABLE
The table and its rows are gone.
Warning: DROP TABLE deletes the table and all its rows, and it can’t be undone.
Step 3: Drop a Table Only If It Exists
Dropping notes again fails, because it’s already gone:
DROP TABLE notes;
ERROR: table "notes" does not exist
IF EXISTS turns the error into a notice, which helps in a script that may run twice:
DROP TABLE IF EXISTS notes;
NOTICE: table "notes" does not exist, skipping
DROP TABLE
Step 4: Drop a Table Another Table Depends On
Here every player’s team_id must be a team_id from teams. That rule is a foreign key, which Part 7 of the PostgreSQL Tutorial explains:
CREATE TABLE teams (team_id integer PRIMARY KEY, name text);
CREATE TABLE
CREATE TABLE players (name text, team_id integer REFERENCES teams);
CREATE TABLE
Now try to drop teams:
DROP TABLE teams;
ERROR: cannot drop table teams because other objects depend on it
DETAIL: constraint players_team_id_fkey on table players depends on table teams
HINT: Use DROP ... CASCADE to drop the dependent objects too.
PostgreSQL refuses, because the foreign key in players depends on teams. CASCADE drops the foreign key too:
DROP TABLE teams CASCADE;
NOTICE: drop cascades to constraint players_team_id_fkey on table players
DROP TABLE
Only the foreign key went, not the players table. It’s still there, with its columns:
\d players
Table "public.players"
Column | Type | Collation | Nullable | Default
---------+---------+-----------+----------+---------
name | text | | |
team_id | integer | | |
That’s how CASCADE treats a foreign key: it removes the constraint, not the other table. A view that uses the dropped table would be dropped completely.
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 league;
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 table that isn’t there | ERROR: table "notes" does not exist | Add IF EXISTS in scripts that may run twice. |
| Dropping a table another table’s foreign key points to | ERROR: cannot drop table teams because other objects depend on it | Drop or change the other table first, or add CASCADE to remove the foreign key. |
Using DROP TABLE to empty a table | The table itself is gone, not only its rows. | To remove only the rows, use DELETE or TRUNCATE, which Part 8 of the PostgreSQL Tutorial shows. |
FAQ
Does CASCADE delete the other table’s rows?
No. For a foreign key, DROP TABLE … CASCADE removes only the constraint: players kept its rows and columns. Don’t confuse it with ON DELETE CASCADE, a foreign key setting that deletes rows.
Can I get a dropped table back?
Not with SQL. Only a backup made before the drop can bring it back.
Conclusion
DROP TABLE deletes a table and all its rows for good. IF EXISTS skips a table that isn’t there, and CASCADE also removes what depends on it, such as another table’s foreign key.
Additional Resources
- PostgreSQL 19 documentation: DROP TABLE (opens in a new tab)
- PostgreSQL 19 documentation: Dependency Tracking (opens in a new tab)
Related
- PostgreSQL Tutorial — Part 6: Create Tables
- How to Rename a Table in PostgreSQL
- How to List Tables 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