Open In App

PostgreSQL – DROP TABLE

PostgreSQL has a DROP TABLE statement that is used to remove an existing table or tables from the database.

Syntax: DROP TABLE [IF EXISTS] table_name [CASCADE | RESTRICT];

Let’s analyze the above syntax:



Example 1:
We will remove the author table from our database using the below statement:

DROP TABLE author;

PostgreSQL issues an error because the author table does not exist.


To avoid this error, you can use the IF EXISTS parameter as follows:



DROP TABLE IF EXISTS author;

This will lead to PostgreSQL issuing a notice instead of an error.

Output:

Example 2:
Here we will remove a table that already exists in our database. To check for all available tables in the database use the below command:

\dt

It will list all existing table as below:


Here we will remove the categories table using the below statement:

DROP TABLE categories;

Now check the list of tables again to verify the removal using the below command:

\dt

Output:

Article Tags :