PostgreSQL DROP DATABASE
Summary: in this tutorial, you will learn how to use the PostgreSQL DROP DATABASE
statement to drop a database.
Introduction to PostgreSQL DROP DATABASE statement
The DROP DATABASE
statement deletes a database from a PostgreSQL server.
Here’s the basic syntax of the DROP DATABASE
statement:
In this syntax:
- First, specify the database name that you want to remove after the
DROP DATABASE
keywords. - Second, if you delete a non-existing database, PostgreSQL will issue an error. To prevent the error, you can use the
IF EXISTS
option. In this case, PostgreSQL will issue a notice instead. - Third, the
DROP DATABASE
statement will fail if there are active connections to the target database unless you use theFORCE
option. TheFORCE
option will attempt to terminate all existing connections to the target database.
The DROP DATABASE
statement deletes the database from both catalog entry and data directory. Since PostgreSQL does not allow you to roll back this operation, you should use it with caution.
To execute the DROP DATABASE
statement, you need to be the database owner.
Additionally, you cannot execute the DROP DATABASE
statement while connecting to the target database. In this case, you can connect to the default postgres
database or use the dropdb
utility before executing the DROP DATABASE
statement.
The dropdb
is a command-line utility that allows you to drop a database. The dropdb
program executes the DROP DATABASE
statement behind the scenes.
PostgreSQL DROP DATABASE statement examples
Let’s take some examples of using the DROP DATABASE
statement.
Setting up sample databases
We’ll create some databases for the demonstration purposes:
1) Basic DROP DATABASE statement example
First, open Command Prompt on Windows or Terminal on Unix-like systems and connect to the PostgreSQL server using psql:
Second, drop the database hr
using the following DROP DATABASE
statement:
2) Removing a non-existing database example
The following example attempts to drop a database that does not exist:
PostgreSQL will issue the following error:
If you use the IF EXISTS
option, PostgreSQL will issue a notice instead:
Output:
3) Drop a database that has active connections example
First, establish a connection to the PostgreSQL server using the psql
tool:
Next, open the second connection to the PostgreSQL server. You can use psql, pgAdmin, or any PostgreSQL client tool.
Then, attempt to delete the test
database from the first session:
PostgreSQL issues an error:
The output indicates that the test
database is being accessed by other users.
To drop the database that has active connections, you can use the FORCE
option.
After that, find the connections to the test
database by retrieving data from the pg_stat_activity
view:
Output:
The test
database has one connection from localhost
. Therefore, it’s safe to terminate this connection and remove the database.
Finally, terminate the connections to the test
database and drop it using the WITH (FORCE)
option:
Output:
Summary
- Use the PostgreSQL
DROP DATABASE
statement to drop a database.