PostgreSQL - DROP Table

PostgreSQL - DROP Table

PostgreSQL

Share

In this tutorial we will learn to list tables inside a database and then delete tables in PostgreSQL.

In the previous tutorial we learned how to create tables in PostgreSQL. Feel free to check that out.

For this tutorial we will use the postgres_project database that we created in the database tutorial.

Alright, login to your local PostgreSQL database and connect to the postgres_project database.

$ psql -U yusufshakeel -p 5432 postgres_project
psql (12.1)
Type "help" for help.

postgres_project=#

Table of content

Show all the tables

To show all the tables inside the postgres_project database we use the \dt command.

postgres_project=# \dt
          List of relations
 Schema |   Name   | Type  |  Owner   
--------+----------+-------+----------
 public | comments | table | postgres
 public | employee | table | postgres
(2 rows)

Show the create statement of a table

To get the create statement of a table we use the \d table_name command.

In the following example we are listing out the employee table details.

postgres_project=# \d employee
                            Table "public.employee"
     Column     |            Type             | Collation | Nullable | Default 
----------------+-----------------------------+-----------+----------+---------
 employeeId     | character varying(20)       |           | not null | 
 firstName      | character varying(100)      |           | not null | 
 lastName       | character varying(100)      |           | not null | 
 score          | integer                     |           |          | 0
 lastModifiedAt | timestamp without time zone |           |          | 
 createdAt      | timestamp without time zone |           | not null | 
Indexes:
    "employeeId_employee_pk" PRIMARY KEY, btree ("employeeId")

Rename a table

To rename a table we use the ALTER TABLE command.

In the following example we are renaming table temp to temp_v1.

# alter table "temp" rename to "temp_v1";

Drop a table

To drop a table in PostgreSQL we use the DROP TABLE table_name command.

In the following example we are dropping table temp_v1.

# drop table "temp_v1";
Share