PostgreSQL Drop Database with Examples

July 11, 2024


PostgreSQL offers two methods to drop a database: the DROP DATABASE statement or the dropdb shell utility.

Removing unused databases is good practice and helps keep the workspace clean. However, 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.

PostgreSQL DROP DATABASE with Examples


DROP DATABASE Statement Syntax

The syntax for the statement is:

DROP DATABASE [database_name];

The statement removes the directory containing the database information and the catalog entries. If a database is in use or non-existent, the command does not execute.

Important: Only the database owner can delete a database.

Drop an Existing Database

To drop an existing database:

1. Connect to PostgreSQL:

sudo -i -u postgres psql
sudo -i -u postgres psql terminal output

The terminal changes to the Postgres shell.

2. List all the databases with:

postgres list of databases output

The output shows a table listing all databases.

3. To drop the database in PostgreSQL, locate the database name from the list and provide it in the following command:

DROP DATABASE [database_name];
postgres drop database output

The dropped database no longer appears in the listing.

Drop a Non-Existing Database

If a database doesn't exist, the DROP DATABASE statement shows an error message:

drop database example error

Add the IF EXISTS option to check if a database exists before deleting it. If the database exists, the statement drops the database. Otherwise, it shows an informative notice. The syntax is:

DROP DATABASE IF EXISTS [database_name];
drop database does not exist message

Use this method to perform a check and avoid code errors.

Drop a Database With Active Connections

The DROP DATABASE method does not remove the database if it is in use. Instead, the terminal prints an error message stating that a database session is open:

drop database error session

Add the WITH (FORCE) to close the session and delete the database forcefully:

DROP DATABASE [database_name] WITH (FORCE);
drop database with force

If possible, Postgres closes the user's session and deletes the database forcefully.

Note: The WITH (FORCE) option is available in PostgreSQL version 13 and higher.

dropdb Utility

The dropdb shell utility is a wrapper for the DROP DATABASE command. Although the utility is identical in effect, dropdb allows the removal of databases remotely. It is the preferred method when performing commands on a remote database server.

The syntax is:

dropdb [connection_parameters] [options] [database_name]

For example, the -i option runs the command in interactive mode, and -e prints the query command:

dropdb -i -e example
dropdb -i -e example

Because of the -i tag, the program asks for confirmation before deleting the database. Press y to confirm the deletion or n to cancel it.

dropdb Options

The table below shows all the possible options when using the dropdb utility:

Prints the commands that dropdb sends to the server.
Attempts to terminate all current connections before dropping the database.
Prompts verification before executing database deletion.
Shows the utility version.
--if-existsPrints a notice instead of an error if the database does not exist.
Shows the help menu.
-h [host]
Specifies the machine's hostname where the server is running.
-p [port]
Specifies the TCP port where the server is listening.
-U [username]
--username [username]
Connects as the specified user.
Doesn't issue the password prompt. Useful for batch and script jobs.
Forces a password prompt.
--maintenance-db=[database_name]Specifies the database name connection.


This guide showed how to drop a Postgres database using two different methods. Since both methods have use cases, it's best to know their syntax and uses.

Next, read our guide on deleting a Postgres user to see how to drop a user in multiple ways, or consider learning about the different data types in PostgreSQL.

Was this article helpful?
Milica Dancuk
Milica Dancuk is a technical writer at phoenixNAP with a passion for programming. With a background in Electrical Engineering and Computing, coupled with her teaching experience, she excels at simplifying complex technical concepts in her writing.
Next you should read
PostgreSQL SELECT Statement {Syntax + Examples}
November 9, 2021

Follow this tutorial to learn how to use the SELECT statement in PostgreSQL. The statement allows you to easily process data and format the output according to your needs.
Read more
How to Export a PostgreSQL Table to CSV
March 17, 2020

Learn how to export a PostgreSQL table to a .csv file. This feature is especially helpful when transferring the table to a different system or importing it to another database application. The guide shows you how to use either...
Read more
How to Install SQL Workbench for PostgreSQL
March 13, 2020

Save time and effort by managing different database systems with a single tool. Find out how to set up SQL Workbench to connect to a PostgreSQL database with four (4) easy steps. Use SQL Workbench...
Read more
PostgreSQL Vs MySQL: A Detailed Comparison
March 30, 2023

Explore the differences between the two most widely used database management systems. PostgreSQL and MySQL are both excellent database solutions, and well suited for most workloads. However, small differences...
Read more