How to show tables in PostgreSQL

March 14, 2024 / How-to Guide

This article explains how to show tables in PostgreSQL. Displaying tables in PostgreSQL is essential for database administrators and developers to understand the structure and contents of a database, facilitating data management and querying operations.

Follow the steps:

Here are three solutions, ordered from fastest and most specific to PostgreSQL, to slowest but most generic-

  1. PSQL-
    1. Assume your database is called db and you are running psql. First, select (\c) the database to work with, and then show (\d) all its tables (\t).
      \c db;
      \dt;
    2. To run psql you can start it from a terminal. The command below assumes your username is Postgres and logs into the db database directly:
      psql db -U postgres;
    3. You can also use the command \d in psql, instead of \dt, to show all tables, views, sequences, roles, and other database objects. For help on the command, enter \? and page down to see all the variants of \d. Push q to quit the help.
    4. You might want to show all tables in a terminal. This can be of use when running shell scripts to automate database tasks. The following terminal command (-c) will display all tables in the db database without having to type in psql.
      psql -U postgres -c "\dt" db;
  2. PostgresSQL SQL-
    1. If you are using a general SQL app instead of psql, the following command will work. It will work in psql too.
      SELECT * FROM pg_catalog.pg_tables WHERE schemaname='public';
  3. Generic SQL-
    1. Finally, this generic command will work across multiple database servers, including Postgres:
      SELECT table_schema || '.' || table_name
      
      FROM information_schema.tables
      
      WHERE table_type = 'BASE TABLE'
      
      AND table_schema NOT IN ('pg_catalog', 'information_schema');

This way, you can show tables in PostgreSQL. If you face any difficulty, contact our support team, they are available 24×7.

Spread the love