Sentry Answers>Postgres>

How to show tables in PostgreSQL?

How to show tables in PostgreSQL?

Richard C.

The ProblemJump To Solution

In PostgreSQL, how do you show all tables for a database?

The Solution

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

PSQL

Assume your database is called db and you are running psql. First, choose (\c) the database to work with, then display (\d) all its tables (\t).

Click to Copy
\c db; \dt;

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:

Click to Copy
psql db -U postgres;

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.

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.

Click to Copy
psql -U postgres -c "\dt" db;

PostgresSQL SQL

If you are using a general SQL app instead of psql, the following command will work. It will work in psql too.

Click to Copy
SELECT * FROM pg_catalog.pg_tables WHERE schemaname='public';

Generic SQL

Finally, this generic command will work across multiple database servers, including Postgres:

Click to Copy
SELECT table_schema || '.' || table_name FROM information_schema.tables WHERE table_type = 'BASE TABLE' AND table_schema NOT IN ('pg_catalog', 'information_schema');
  • ResourcesWhat is Distributed Tracing
  • Syntax.fm logo
    Listen to the Syntax Podcast

    Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.

    SEE EPISODES

Loved by over 4 million developers and more than 90,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.

© 2024 • Sentry is a registered Trademark
of Functional Software, Inc.