Introduction
PostgreSQL offers two command-line methods to drop a database - using theDROP DATABASE
statement or a shell utility.Removing unused databases is good practice and helps keep the workspace clean. However, keep in mind that deleting an existing PostgreSQL database removes all catalog entries and data for that database.Continue reading to learn how to drop a database in PostgreSQL.
Prerequisites
- PostgreSQL 10 or higher installed and configured (follow our guide for Ubuntu or Windows; if already installed, check the PostgreSQL version on the system).
- Access to the terminal with sudo privileges.
DROP DATABASE Statement
Important: ONLY the database owner can delete a database.
DROP DATABASE <database name>;
The command removes the directory containing the database information and the catalog entries. Only the database owner can execute the DROP DATABASE
command. If anyone is currently using the database, the command does not execute.To see how DROP DATABASE
works, do the following:1. Open the terminal (CTRL+ALT+T).2. Connect to PostgreSQL:sudo -i -u postgres psql

CREATE DATABASE example;

l

DROP DATABASE example;

l
The example database no longer appears in the list.IF Exists
TheIF EXISTS
option is open for all versions where DROP DATABASE
is available. The full command syntax with the IF EXISTS
option is as follows:DROP DATABASE IF EXISTS <database name>;
The option first checks if a database exists before deleting it. If a database exists, the command drops the database. However, if a database doesn't exist, the command prints an informative notice message.To test how the command works, follow the steps below:1. Create an example database:CREATE DATABASE example;
2. Drop the database using the IF EXISTS
option:DROP DATABASE IF EXISTS example;

DROP DATABASE
if the database does exist.3. The database is no longer available. Rerun the command to see the output:DROP DATABASE IF EXISTS example;

IF EXISTS
and omitting the option, run the following command:DROP DATABASE example;

DROP DATABASE
without the IF EXISTS
option on a non-existent database throws an error message.WITH (FORCE)
TheWITH (FORCE)
option is available in PostgreSQL version 13 and higher.The DROP DATABASE
method won't remove the database if it's in use. If the database is in use, the terminal prints an error that a database session is open.
WITH (FORCE)
option to forcefully close the session and delete the database:DROP DATABASE <database name> WITH (FORCE);

The dropdb Utility
The dropdb shell utility is a wrapper for theDROP DATABASE
command. Effectively, the two methods are identical. However, dropdb offers additional options including removing databases remotely.The basic syntax is:dropdb <connection parameters> <options> <database name>
Options
The table below shows all the possible options when using the dropdb utility.Option | Type | Description |
---|---|---|
-e
--echo | Option | Prints the commands that dropdb sends to the server. |
-f
--force | Option | Attempts to terminate all current connections before dropping the database. |
-i
--interactive | Option | Prompts verification before executing database deletion. |
-V
--version | Option | The console prints the utility version. |
--if-exists | Option | Prints a notice instead of an error if the database does not exist. |
-?
--help | Option | Show the help menu. |
-h <host>
--host=<host> | Connection parameter | Specifies the hostname of the machine where the server is running. |
-p <port>
--port=<port> | Connection parameter | Specifies the TCP port where the server is listening. |
-U <username>
--username <username> | Connection parameter | Connect as the specified user. |
-w
--no-password | Connection parameter | Never issue the password prompt. Useful for batch and script jobs when no user is present. |
-W
--password | Connection parameter | Force password prompt. Without the option, the server loses the connection attempt if a password is necessary. |
--maintenance-db=<database name> | Connection parameter | The option specifies the database name connection. |
-i
and -e
options:dropdb -i -e example
The program asks for confirmation before the deletion because of the -i
tag.