How to Drop a Table in Postgres

We can drop an unnecessary table permanently from a database to free up the storage. Dropping a table is an easy task in PostgreSQL that can be done using two different approaches. The DROP TABLE statement can be utilized to delete/drop a Postgres table. Also, this can be done manually. Let’s get into the details of the following two methods:

  1. Dropping a table using the Postgres Query
  2. Dropping a table manually

Method 1: Dropping a Table Using the Postgres Query

We can drop a table using the DROP TABLE query in Postgres. The basic syntax to use this query is:

DROP TABLE t_name

Syntax Description

  • The DROP TABLE clause is used to drop a table in Postgres.
  • The table’s name that we want to drop is specified after the DROP TABLE command.

Let’s take the below example to understand and implement the concept.

Example: Drop a Table in Postgres Using Query

We can write the same query to delete a table in psql and pgAdmin.The query to drop a table “fee_invoice” can be written as:

DROP TABLE fee_invoice;

The above query will drop the “fee_invoice” table which can be verified from the output.

How to Drop va Table in Postgres-01

We can also verify if the table has been dropped or not by executing the SELECT statement like this:

SELECT * FROM fee_invoice;

The output of this query will clearly say that the table “fee_invoice” does not exist.

How to Drop va Table in Postgres-02

This means the table has been dropped. We can also drop multiple tables using the DROP TABLE statement. The basic syntax looks like this:

DROP TABLE tab1, tab2,... tabn;

Where in the query you have to specify the tables you want to drop. The query will delete/drop all the tables that will be specified in the query.

This is how we can delete tables in Postgres using queries.

Method 2: Dropping a Table Manually

Manually deleting a table is an easy task. To do this, perform the following outlined steps.

Step 1: Drop The Table

Go to the database in which the table exists and expand the table option. To delete a specific table, right-click on that specific table from the side panel. Select “delete\drop” from the drop-down menu that appears.

How to Drop va Table in Postgres-03

Step 2: Confirm Table Deletion

After clicking on the “delete\drop” option, a window will appear asking for confirmation to drop/delete the table. Press “Yes” to delete the table.

How to Drop va Table in Postgres-04

Step 3: Verify Table Deletion

The table will be deleted when you click the drop button. We can see from the side panel that the selected table will no longer be available.

How to Drop va Table in Postgres-05

We can observe that this database has only 2 tables. The fee_invoice table has been deleted/dropped from the database.

Conclusion

We can drop/delete the unwanted and unnecessary tables by using the DROP TABLE clause. The DROP TABLE statement can help us drop a table or multiple Postgres tables. We can also drop a table manually in Postgres. This tutorial has illustrated the methods to drop/delete a table in PostgreSQL.