Skip to main content
0 reactions

How to Drop a Table in PostgreSQL

Featured card for How to Drop a Table in PostgreSQL, with a terminal showing DROP TABLE notes, IF EXISTS with no error, teams dropped with CASCADE, and the players table kept.
How to Drop a Table in PostgreSQL

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

SyntaxSQL
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:

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

Step 2: Drop a Table

league=#SQL
CREATE TABLE notes (body text);
OutputPlain text
CREATE TABLE
league=#SQL
DROP TABLE notes;
OutputPlain text
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:

league=#SQL
DROP TABLE notes;
OutputPlain text
ERROR:  table "notes" does not exist

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

league=#SQL
DROP TABLE IF EXISTS notes;
OutputPlain text
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:

league=#SQL
CREATE TABLE teams (team_id integer PRIMARY KEY, name text);
OutputPlain text
CREATE TABLE
league=#SQL
CREATE TABLE players (name text, team_id integer REFERENCES teams);
OutputPlain text
CREATE TABLE

Now try to drop teams:

league=#SQL
DROP TABLE teams;
OutputPlain text
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:

league=#SQL
DROP TABLE teams CASCADE;
OutputPlain text
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:

league=#Plain text
\d players
OutputPlain text
               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:

league=#Plain text
\c postgres
OutputPlain text
You are now connected to database "postgres" as user "postgres".
postgres=#SQL
DROP DATABASE league;
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 table that isn’t thereERROR: table "notes" does not existAdd IF EXISTS in scripts that may run twice.
Dropping a table another table’s foreign key points toERROR: cannot drop table teams because other objects depend on itDrop or change the other table first, or add CASCADE to remove the foreign key.
Using DROP TABLE to empty a tableThe 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

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