PostgreSQL is an open-source relational database system. One of the common tasks of a PostgreSQL developer is to show all tables from a database. PostgreSQL provides two ways to list all tables from a database.
Table of Contents
PSQL Command Line Method
PSQL allows us to interact through a terminal-based front-end. To list all the tables in a database, use the below command.
\dt
In the above image, we are first connecting to the demo database using the \c
command. Then we use the \dt
command to list all the tables. The \dt
command provide the below information in a table format.
- Schema: The schema of the table
- Name: Name of the table
- Type: Table type
- Owner: Owner of the table
If you need the size of the table, then use the \dt+
command as shown below.
\dt+
In the 7th column, PostgreSQL provides the size of the table.
Show all tables using the “information_schema.tables” Table
PostgreSQL provides a special table that has all the information about the current database. The table name is information_schema.tables. This table contains below information.
- Table Database Name
- Schema
- Table Name
- Table Type
To show all tables in a current database, use the below query.
SELECT * FROM information_schema.tables
WHERE table_schema='public' AND
table_type='BASE TABLE'
Summary
PostgreSQL provides two ways to show all the tables. 1) \dt 2) information_schema.tables. To get the size of the table use the \dt+ command.