PostgreSQL Tutorial PostgreSQL Advanced PostgreSQL Database Account Management PostgreSQL References
PostgreSQL Tutorial PostgreSQL Advanced PostgreSQL Database Account Management PostgreSQL References

PostgreSQL - DROP TABLE



The PostgreSQL DROP TABLE statement is used to delete a table from the database. It drops all the data, indexes, triggers, constraints and permission specifications for the specified table.

Note: Be careful before dropping a table. Once deleted, all information stored in that table will be lost forever!.

Syntax

The syntax of using DROP TABLE statement in PostgreSQL is given below:

DROP TABLE [IF EXISTS] table_name;

The IF EXISTS is an optional parameter that conditionally drops table only if it exists on the database. If a table is deleted which does not exist, it will raise an error.

Example:

Consider a database containing a table called Employee with the following records:

EmpIDNameCityAgeSalary
1JohnLondon253000
2MarryNew York242750
3JoParis272800
4KimAmsterdam303100
5RameshNew Delhi283000
6HuangBeijing282800

The description of the table can be checked using \d command as shown below:

\d Employee;

This will produce the result as shown below:

                     Table "public.employee"
 Column |          Type          | Collation | Nullable | Default 
--------+------------------------+-----------+----------+---------
 empid  | integer                |           | not null | 
 name   | character varying(255) |           | not null | 
 city   | character varying(100) |           |          | 
 age    | integer                |           |          | 
 salary | numeric(18,2)          |           |          | 
Indexes:
    "employee_pkey" PRIMARY KEY, btree (empid)

To delete this table, the following statement can be used:

DROP TABLE Employee;

After dropping the table, the \d command will throw following error:

\d Employee;	
Result: psql:commands.sql:1: error: Did not find any relation named "Employee".