Summary: in this tutorial, you will learn how to delete existing database by using PostgreSQL DROP DATABASE statement.
Introduction to PostgreSQL DROP DATABASE statement
Once a database is no longer needed, you can delete it by using the DROP DATABASE
statement. The following illustrates the syntax of the DROP DATABASE
statement:
1 | DROP DATABASE [IF EXISTS] name; |
To delete a database:
- Specify the name of the database that you want to delete after the
DROP DATABASE
clause. - Use
IF EXISTS
to prevent an error from removing a non-existent database. PostgreSQL will issue a notice instead.
The DROP DATABASE
statement deletes catalog entries and data directory permanently. This action cannot be undone so you have to use it with caution.
Only the database owner can execute the DROP DATABASE
statement. In addition, you cannot execute the DROP DATABASE
statement if there is any active connection to the database. You have to connect to another database e.g., postgresql
to execute the DROP DATABASE
statement.
PostgreSQL also provides a utility program named dropdb
that allows you to remove a database. The dropdb
program executes the DROP DATABASE
statement behind the scenes.
Delete a database that has active connections
To delete the database that still has active connections, you can follow the steps below:
First, find the activities that are taken place against the target database, you can query the pg_stat_activity
view as the following query:
1 2 3 4 5 6 | SELECT * FROM pg_stat_activity WHERE datname = 'target_database'; |
Second, terminate the active connections by issuing the following query:
1 2 3 4 5 6 | SELECT pg_terminate_backend (pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = 'target_database'; |
Notice that if you use PostgreSQL version 9.1 or earlier, use the procpid
column instead of the pid
column because PostgreSQL changed procid
column to pid
column since version 9.2
Third, execute the DROP DATABASE
statement:
1 | DROP DATABASE target_database; |
PostgreSQL DROP DATABASE examples
We will use the databases created in the PostgreSQL create database tutorial for the demonstration. If you don’t have these databases available, you can create them by executing the following statements:
1 2 | CREATE DATABASE hrdb; CREATE DATABASE testdb1; |
Delete database that has no active connection example
To remove the hrdb
database, use the hrdb
owner to connect to a database other than hrdb
database e.g., postgres
and issue the following statement:
1 | DROP DATABASE hrdb; |
PostgreSQL deleted the hrdb
database.
Delete database that has active connections example
The following statement deletes the testdb1
database:
1 | DROP DATABASE testdb1; |
However, PostgreSQL issued an error as follows:
1 2 3 | ERROR: database "testdb1" is being accessed by other users SQL state: 55006 Detail: There is 1 other session using the database. |
To delete the testdb1 database, you need to follow the steps as described in the above section.
First, query the pg_stat_activity
view to find what activities are taking place against the testdb1
database:
1 2 3 4 5 6 | SELECT * FROM pg_stat_activity WHERE datname = 'testdb1'; |
The testdb1
database has 1 connection from localhost
therefore it is safe to terminate this connection and remove the database.
Second, terminate the connection to the testdb1
database by using the following statement:
1 2 3 4 5 6 | SELECT pg_terminate_backend (pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = 'testdb1'; |
Third, issue the DROP DATABASE
command to remove the testdb1
database:
1 | DROP DATABASE testdb1; |
PostgreSQL deleted the testdb1
permanently.
In this tutorial, you have learned how to use the PostgreSQL DROP DATABASE
statement to delete a database. In addition, you also learned how to delete a database that has active connections.