Introduction to Postgres DROP Database
Whenever the data in the database is not important and no necessary to store it anymore further or is an empty database that you wish to delete then you can delete that database completely using the DROP Database command or one available utility for that named dropdb. Whenever we delete that means to drop a database at that time what happens is the whole directory which is there for that database is permanently deleted and its related information about the database to be deleted stored in the catalog entries is also deleted permanently. There is no way to rollback this query, hence you have to be very much sure and careful while using these commands.
In this article, we will learn how we can delete the database completely using available two methods of DROP Database statement and dropdb utility program one by one.
Syntax:
DROP DATABASE [IF EXISTS] nameOfTheDb;
The above syntax contains the query statements DROP Database and other parameters in it can be explained in the following manner –
- IF EXISTS – It is an optional parameter. This keyword can be used to prevent any error which is resulted while using the above command when there is no database existence in the current database server having the name “nameOfTheDb”. If the “IF EXISTS” keyword is used in your query and none of the database named “nameOfTheDb” is present in the current server then it will simply notify you about the same and won’t result in any error.
- nameOfTheDb – It is the name of the database that you wish to delete or drop and is located in the current database.
Notes:
There are few things to be considered while dropping a database and understanding its behavior.
- We cannot delete the database if the database we are deleting has any active sessions associated with it.
- Further, the database can only be deleted by the owner of the database.
- One more thing to keep in mind that the drop database query statement cannot be executed for the same database you are connected to right now.
- Once the database is deleted, the actions cannot be undone. So be more cautious while doing so.
Example
We can check all the databases present in our database server using the query \l which results into the following output currently for my server –
\l
As can be seen, there is no database named sample present in our database server.
Let us understand how we can use this command with the help of an example. The first thing that we need to do is open our psql command prompt and then execute our statement. Suppose, we want to drop the database named “sample”. So, my query statement will be as follows –
DROP DATABASE sample;
It will result in the following output –
As can be seen, it has resulted in an error saying that there is no database named “sample” in our current database server.
Now what will be output if we use IF EXISTS for the same command like –
DROP DATABASE IF EXISTS sample;
It will result in the following output –
We can see that instead of an error it has thrown a notice to inform us that there does not exist such a database.
Let us create a database named “sample” and then perform the above query. We can use this command for creating a new database for our purpose –
CREATE DATABASE sample;
that will result in the following output –
Let us check whether it is created successfully with the help of command \l which result as follows –
So, our database now exists. This means when we run any of the above DROP commands, it won’t result in error or notice. Let us check the output by firing the query itself.
DROP DATABASE sample;
now results in –
Let us verify by checking all databases.\l gives following output –
This means that our database is dropped successfully.
Let us try deleting a database with active connections. In this example, we will drop the database demo which already exists. You can even create a new database and try deleting it. For database o have active connections, then we will open a new terminal with psql prompt and then connect to demo database using \c demo; command.
\c demo;
Now, returning back to my previous terminal, we will try to drop demo database.
DROP DATABASE demo;
As can be seen, it shows an error saying one session is already using the database named demo. Hence, it cannot be deleted. In such cases, we can drop the database by first checking all the activities associated with it and terminating them. We can see all the activities of the database from table pg_stat_activity.
Let us fire the following query –
SELECT * FROM pg_stat_activity WHERE datname = 'demo';
that give output –
One session is associated with the demo database. We can terminate all of them by using the query –
SELECT pg_terminate_backend (pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = 'demo';
Let us drop our demo db now.
DROP DATABASE demo;
And \l command now results in –
Hence, our database is dropped successfully.
dropdb command -
We can even use the dropdb command for deleting the database. It works similarly to the DROP DATABASE statement. The advantage is that we can drop the database remotely using dropdb functionality but it is necessary to be the owner of the database, we are trying to drop. the syntax is as follows –
Dropdb [option.] nameOfTheDb;
- nameOfTheDb – It is the name of the database that you wish to delete or drop and is located in the current database.
Options can be any of the following –
Option | Description |
-e | Echo commands. |
-i | Whether to display verification prompt |
-V | For printing dropdb version |
–help | For getting help |
-h host | Specify host of current system |
-p port | Socket used by current established connections. |
–if exists | Whether to show notice instead of error if database does not exists. |
-U username | User name using which to connect database. |
-w | If no password prompt required to be ask. |
-W | If password prompt required to be ask before droping Database. |
maintenance db-=dbname | Name od datbase to connect to for droping target database. |
Example of Postgres DROP Database
dropdb -p 5432 -i -e educational_platform;
Note that dropdb command is shell command and not psql. You can open psql command and fire \l command to check whether a database is deleted or not.
So, our database educational_platform is deleted successfully.
Conclusion
We can drop our database either by using the DROP Database statement or dropdb utility. Be careful while doing so as action cannot be undone.
Recommended Articles
This is a guide to Postgres DROP Database. Here we discuss introduction of Postgres DROP Database, syntax, parameters, and examples with code implementation. You may also have a look at the following articles to learn more –