How to show all tables in PostgreSQL?

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.

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
\dt command for show all tables for PSQL

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.

  1. Schema: The schema of the table
  2. Name: Name of the table
  3. Type: Table type
  4. Owner: Owner of the table

If you need the size of the table, then use the \dt+ command as shown below.

\dt+
\dt+ command to show sizes for each table

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.

  1. Table Database Name
  2. Schema
  3. Table Name
  4. 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'
List all tables using information_schema.tables

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.