LinuxCommandLibrary

dropdb

Remove a PostgreSQL database

TLDR

Destroy a database

$ dropdb [database_name]
copy

Request a verification prompt before any destructive actions
$ dropdb [[-i|--interactive]] [database_name]
copy

Connect with a specific username and destroy a database
$ dropdb [[-U|--username]] [username] [database_name]
copy

Force a password prompt before connecting to the database
$ dropdb [[-W|--password]] [database_name]
copy

Suppress a password prompt before connecting to the database
$ dropdb [[-w|--no-password]] [database_name]
copy

Specify the server host name
$ dropdb [[-h|--host]] [host] [database_name]
copy

Specify the server port
$ dropdb [[-p|--port]] [port] [database_name]
copy

Attempt to terminate existing connections before destroying the database
$ dropdb [[-f|--force]] [database_name]
copy

SYNOPSIS

dropdb [connection-option...] [option...] DBNAME

PARAMETERS

-c, --clean
    Clean (flush) database contents before dropping by terminating connections

-e, --echo
    Echo the generated DROP commands sent to server

-f, --force
    Ignore errors (e.g., database does not exist)

-i, --interactive
    Prompt before any destructive action

--if-exists
    Do not error if database does not exist

--no-clean
    Do not clean database before dropping (default)

--no-owner-check
    Skip ownership verification

-h HOSTNAME, --host=HOSTNAME
    Database server host or socket directory

-p PORT, --port=PORT
    Database server port

-U NAME, --username=NAME
    Database user name

-w, --no-password
    Never prompt for password

-W, --password
    Force password prompt

--role=ROLE
    Set current role before dropping

--maintenance-db=DBNAME
    Maintenance database to connect to

-V, --version
    Output version information

-?, --help
    Show help message

DESCRIPTION

dropdb is a command-line utility in PostgreSQL for removing a specified database from the server. It executes the SQL DROP DATABASE statement, permanently deleting the database, all its tables, schemas, data, and associated objects.

Only superusers or the database owner can perform this action. The target database must have no active connections; otherwise, the command fails unless --clean is used to terminate them. dropdb supports connection options for remote servers and is ideal for automation in deployment scripts, CI/CD pipelines, or test environment cleanup.

It connects via libpq, using defaults like local Unix socket, port 5432, and current user. Errors occur if privileges are insufficient or if the database is in use. Always ensure backups (e.g., via pg_dump) before use, as deletion is irreversible.

This tool simplifies database lifecycle management but demands caution due to its destructive nature.

CAVEATS

Irreversible deletion of all data; requires superuser or owner privileges; fails if connections exist without --clean; cannot drop template0, template1, or postgres by default; use pg_dump for backups first.

EXIT STATUS

0 on success, 1 on SQL error, 2 on usage error, 3 if canceled by interactive prompt

EXAMPLES

dropdb mydb
dropdb -h localhost -U postgres --clean mydb
dropdb --if-exists testdb

ENVIRONMENT

Respects PGHOST, PGPORT, PGUSER, PGPASSWORD, PGOPTIONS, PGSSLMODE, etc.

HISTORY

Part of PostgreSQL since version 6.0 (1997); developed and maintained by PostgreSQL Global Development Group for streamlined DB management.

SEE ALSO

createdb(1), psql(1), pg_dump(1), DROP DATABASE(7)

Copied to clipboard