Drop a database and all of its objects

Destructive
What do risk levels mean?
Read-only
Inspects state without changing anything.
Low risk
Reversible with a routine follow-up command.
Medium risk
Changes state; undo path documented.
Destructive
Deletes or overwrites; confirmation required.

Permanently removes a database and everything inside it — tables, views, functions, and data — after first confirming what will be lost.

postgresqldropdatabasedelete

Confirmation required

This recipe is destructive and requires confirmation — confirm you understand the impact before running any command below.

Parameters

Name of the database to drop.

Commands

Count user tables that will be destroyed

psql -d <database_name> -c "SELECT count(*) AS table_count FROM information_schema.tables WHERE table_schema = 'public';"

Permanently remove the database and all contained objects

dropdb <database_name>

Verification

psql -c "\l" | grep -c <database_name>

The count is zero — the database no longer appears in the listing.

Undo

Not undoable

A dropped database cannot be recovered without a backup. Always run a backup before dropping a database.

Pitfalls

  • Active connections to the database will prevent the drop; terminate them first with pg_terminate_backend.
  • Template databases and the connected database cannot be dropped.
  • The database owner role continues to exist; only the database itself is removed.

Related