PostgreSQL
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.
postgres_project
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
To show all the tables inside the postgres_project database we use the \dt command.
\dt
postgres_project=# \dt List of relations Schema | Name | Type | Owner --------+----------+-------+---------- public | comments | table | postgres public | employee | table | postgres (2 rows)
To get the create statement of a table we use the \d table_name command.
\d table_name
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")
To rename a table we use the ALTER TABLE command.
ALTER TABLE
In the following example we are renaming table temp to temp_v1.
# alter table "temp" rename to "temp_v1";
To drop a table in PostgreSQL we use the DROP TABLE table_name command.
DROP TABLE table_name
In the following example we are dropping table temp_v1.
# drop table "temp_v1";