PostgreSQL Drop/Delete Table
In PostgreSQL, we can use the Drop table command to delete the existing table or which we don’t need anymore. This command deletes the complete data of a table along with the whole structure or definition permanently from the database.
So, we must be very alert while deleting the table because we cannot recover the lost data after removing it.
In this section, we are going to learn how to drop and delete a table in PostgreSQL.
The syntax for Dropping a table
Here the table name parameter is used to define the name of the table, which we will delete from the database. And the drop table is the keyword which is used to drop a table.
In PostgreSQL, we can drop the table in two ways:
- Dropping a table from psql
- Drop table in pgAdmin
Dropping a table from psql
To drop/delete or remove in psql, we are going to follow the below steps:
Step1
We had created the table Student and Department in the previous section of the tutorial. First, we will confirm these tables with the help of below command:
After using the above command, the following output will occur:
Step2
This means that the Student and Department tables are available in the selected database. Thus, we will let drop them with the help of below command:
Step3
And if we again check for the list of relations:
The above message specifies that drop command is executed successfully.
Drop table in pgAdmin
And if we want to drop a table from the pgAdmin, we need to follow the below process:
Step1
Open your pgAdmin and then go the object tree where we will go to the database and then move to the public section under schemas and then select the Employee table which we want to delete or drop.
Step2
Then we will right-click on the selected (Employee) table, and click on the Delete/Drop option from the given drop-down list as we can see in the below screenshot:
Step3
The drop table popup window will appear on the screen, where we will click on the Yes button to drop the Employee table.
Step4
Once we clicked on the Yes button, the table will be deleted automatically, as we can see in the below screenshot that there is no table available in the Table section.