How to show tables in PostgreSQL?

Richard C.

The Problem

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).

\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:

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.

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.

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

Generic SQL

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');

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.

Share on Twitter
Bookmark this page
Ask a questionJoin the discussion

Related Answers

A better experience for your users. An easier life for your developers.

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