This guide explains how to delete PostgreSQL databases using two methods: the SQL command DROP DATABASE
and the dropdb
utility. Both methods are effective, but dropdb
offers slightly more options for managing the deletion process.
The first way to delete a PostgreSQL database is to use the SQL command:
DROP DATABASE <database_name>;
This command deletes the directory containing the database's information and previously stored records. Note the following:
Only the database owner can execute DROP DATABASE.
If someone is connected to the database at the time of execution, the deletion will not proceed.
Let’s walk through deleting a database step by step.
Step 1: Connect to PostgreSQL.
Access PostgreSQL through the terminal:
sudo -i -u postgres psql
Step 2: Create a Test Database:
CREATE DATABASE example_db;
Step 3: Verify the Database Exists.
List all databases to confirm we have successfully created the new one:
\l
This will display a table listing all databases on the server.
Step 4: Delete the Database:
DROP DATABASE example_db;
Step 5: Verify Deletion.
List all databases again to ensure example_db
is no longer in the list:
\l
The table should no longer include the deleted database.
The IF EXISTS
option allows you to check if a database exists before attempting to delete it. The full syntax is as follows:
DROP DATABASE IF EXISTS <database_name>;
If the database exists, the command will delete it. If no database with that name exists, an informational message will be displayed instead of an error.
First, create a sample database:
CREATE DATABASE hostman_db;
Then, delete the newly created database using the IF EXISTS
option:
DROP DATABASE IF EXISTS hostman_db;
If the database exists, the result will be the same as using DROP DATABASE
. The hostman_db
database will no longer be available.
If you run the DROP DATABASE IF EXISTS
command again with the same database name:
DROP DATABASE IF EXISTS hostman_db;
The terminal will display an informational message indicating that the entity doesn't exist.
Finally, using DROP DATABASE
without the IF EXISTS
option will result in a less informative error message:
DROP DATABASE hostman_db;
The WITH (FORCE)
option is available in PostgreSQL version 13 and above. It is used for "forcefully" removing a database that is currently in use. The WITH (FORCE)
option forcibly closes the session and deletes the database.
DROP DATABASE <database_name> WITH (FORCE);
The dropdb
command-line utility is essentially a wrapper around the DROP DATABASE
SQL command. These two methods are identical in terms of functionality, but dropdb
offers additional features that make the process of deleting a PostgreSQL database more convenient and informative.
Its basic syntax is:
dropdb <connection parameters> <options> <database_name>
Flags and Options:
Flag |
Description |
|
Displays all the commands actually executed on the server while using the |
|
Terminates all current connections before deleting the database. |
|
Prompts for confirmation before deleting the database. |
|
Displays the version of the |
|
Provides an informative message instead of an error if the target database does not exist. |
|
Displays the help menu with information about available options. |
|
Specifies the host where the database is running. |
|
Specifies the port number the server is listening to. |
|
Allows connection to the database using the specified username. |
|
Makes the password prompt optional. Useful for batch jobs and scripts. |
|
Forces a password prompt for every operation. |
|
Specifies the name of the maintenance database for the connection. |
If you have cloud databases and need to delete unused entities periodically, you might want the process to be safe and clear to all users. The dropdb
utility handles both of these requirements.
For example, consider the following command:
dropdb -i -e example_db
It will:
First prompt you to confirm the deletion of example_db
.
Display the commands executed during the operation.
To delete a database, you must be the owner of the database and have access to the terminal with sudo privileges.
The choice of deletion method depends on you. There is no fundamental difference between using DROP DATABASE
and the dropdb
utility. It is simply a matter of convenience, as well as the specific scenarios in which you need to delete a database in PostgreSQL.
Hostman offers a high-performance cloud PostgreSQL database solution to meet your needs.