PostgreSQL Drop Table
DROP TABLE permanently removes a table, along with all of its data and structure, from the database.
The DROP TABLE Statement
DROP TABLE table_name deletes the table definition and every row it contains in one step. There's no confirmation prompt and, once the transaction commits, no built-in way to get the data back - which makes DROP TABLE the most destructive statement covered in this section.
Drop a table
DROP TABLE employees;If a table might not exist - for example, in a setup script that could run more than once - dropping it directly raises an error. Adding IF EXISTS makes the statement succeed silently when the table isn't there.
Drop a table only if it exists
DROP TABLE IF EXISTS temp_employees;Dropping Multiple Tables and Dependent Objects
You can drop several tables in one statement by listing their names separated by commas. If other objects - like views or foreign keys in other tables - depend on the table you're dropping, PostgreSQL blocks the statement by default (RESTRICT). Adding CASCADE tells PostgreSQL to drop those dependent objects too.
Drop multiple tables and their dependents
DROP TABLE orders, order_items CASCADE;- Confirm the table name and schema - there's no undo once the transaction commits.
- Search for views, functions, and foreign keys that reference the table.
- Take a backup with pg_dump, or run the DROP inside a transaction you can roll back.
- Check whether CASCADE would remove other objects you still need.
- Update any application code or migrations that reference the dropped table.
Exercise: PostgreSQL Drop Table
What is removed when you run DROP TABLE on a table?