DROP_TABLE(7pg15)
π DROP TABLE
okay bestie, so this one's from the PostgreSQL 15.19 docs and it's literally the "bye forever" button for tables. Buckle up, it's giving destructive main character energy π₯
β¨ SYNOPSIS β¨
DROP TABLE [ IF EXISTS ] name [, ...] [ CASCADE | RESTRICT ]
πΈ DESCRIPTION πΈ
DROP TABLE removes tables from the database. That's it, that's the vibe. π
But ngl, not just anyone can do it β only the table owner, the schema owner, and superuser can drop a table. So if you're getting told no, it's not personal, it's permissions.
And listen π«Ά β if you just wanna empty a table of rows without destroying the table itself, do not drop it. Use DELETE or TRUNCATE instead. Dropping is the "burn the whole house down" option, those are the "just clear out the closet" option.
Here's the tea on what gets cleaned up automatically: DROP TABLE always removes any indexes, rules, triggers, and constraints that exist for the target table. Zero effort needed, they go down with the ship. π’
BUT π β if you're dropping a table that's referenced by a view or a foreign-key constraint of another table, you must specify CASCADE. And the way CASCADE behaves differs by case, so pay attention:
- in the view case, it removes the dependent view entirely
- in the foreign-key case, it only removes the foreign-key constraint, not the other table entirely
iykyk π
π PARAMETERS π
IF EXISTSβ do not throw an error if the table does not exist. A notice is issued in this case. Basically "no drama if it's already gone" β¨nameβ the name (optionally schema-qualified) of the table to drop. Yes bestie, schema-qualified is allowed πCASCADEβ automatically drop objects that depend on the table (such as views), and in turn all objects that depend on those objects (see Section 5.14). It's giving domino effect π₯RESTRICTβ refuse to drop the table if any objects depend on it. This is the default, so like... it's already on your side, protecting you π
β οΈ real talk: CASCADE is a whole chain reaction. It doesn't just take out the first layer of dependents, it goes after everything that depends on those things too. Read Section 5.14 before you get bold, fr fr.
π EXAMPLES π
To destroy two tables, films and distributors:
DROP TABLE films, distributors;
two tables, one command, absolutely ate π
π¦ COMPATIBILITY π¦
This command conforms to the SQL standard β with two little caveats:
- the standard only allows one table to be dropped per command
- apart from the
IF EXISTSoption, which is a PostgreSQL extension
so like, mostly iconic, slightly extra π
β SEE ALSO β
ALTER TABLE(ALTER_TABLE(7))CREATE TABLE(CREATE_TABLE(7))
and that's the whole page, bestie π§π β brought to you by PostgreSQL 15.19, dated 2026, aka the <rainbow>DROP TABLE(7)</rainbow> manual page. now go forth and drop responsibly, mwah π
π¦ where this page comes from
- opensuse-leap 16.0: postgresql15 15.19-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql15 15.19-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql15 15.19-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql15 15.19-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms