Summary: in this tutorial, you will learn how to show tables in PostgreSQL using psql tool and pg_catalog schema.
MySQL offers a popular SHOW TABLES statement that displays all tables in a specific database.
Unfortunately, PostgreSQL does not support the SHOW TABLES statement directly but provides you with alternatives.
Showing tables from PostgreSQL using psql
First, open the Command Prompt on Windows or Terminal on Unix-like systems and connect to the PostgreSQL using psql client tool:
Second, change the current database to the one that you want to show tables:
Note that you can connect to a specific database when you log in to the PostgreSQL database server:
In this command, the -d flag means database. In this command, you connect to the dvdrental database using the postgres user.
Third, use the \dt command from the PostgreSQL command prompt to show tables in the dvdrental database:
Output:
To get more information on tables, you can use the \dt+ command. It will add the size and description columns:
Output:
To show the details of a specific table, you can specify the name of the table after the \d command:
Or
For example, the following shows the structure of the actor table:
Output:
Showing tables using pg_catalog schema
The following statement retrieves the table in PostgreSQL from the pg_catalog.pg_tables view:
Output:
In this query, we use a condition in the WHERE clause to exclude the system tables. If you omit the WHERE clause, you will get many tables including the system ones.
Summary
Use the \dt or \dt+ command in psql to show tables in a specific database.
Use the SELECT statement to query table information from the pg_catalog.pg_tables catalog.