Skip to main content
0 reactions

How to List Tables in PostgreSQL

Featured card for How to List Tables in PostgreSQL, with a terminal showing the list command, the tables notebooks and pens in the public schema, and a note that the plus form adds each table’s size.
How to List Tables in PostgreSQL

To list tables in PostgreSQL, type \dt in psql. It shows every table in the database you’re connected to, with its schema and owner. Here’s how to use it, how to see each table’s size, and a SQL query that lists tables in tools without \dt, such as pgAdmin’s Query Tool.

Every command and output on this page is real, from PostgreSQL 19 on macOS.

Syntax

SyntaxPlain text
\dt [ pattern ]
\dt+ [ pattern ]
SyntaxSQL
SELECT table_name FROM information_schema.tables
WHERE table_schema = 'schema_name';
  • \dt: psql’s command that lists tables. Type it in psql, with no semicolon.
  • pattern: optional. Lists only the tables whose names match, such as n* for the names that start with n, or sales.* for every table in the schema sales.
  • +: also shows each table’s persistence, access method, size on disk and description.
  • information_schema.tables: a view in every database with one row for each table (and view). table_schema is the schema to look in, usually public.
  • [ ] marks the optional part.

See psql (opens in a new tab) (the \d commands) and tables (opens in a new tab) in the information schema, in the PostgreSQL documentation.

The Example

A stationery shop keeps two tables: pens and notebooks.

Step 1: Create a Test Database

Connect to your server with psql as the postgres user, as Part 2 of the PostgreSQL Tutorial shows. psql connects you to the postgres database, and its prompt is postgres=#.

Create a database for the example and connect to it:

postgres=#SQL
CREATE DATABASE stationery;
OutputPlain text
CREATE DATABASE
postgres=#Plain text
\c stationery
OutputPlain text
You are now connected to database "stationery" as user "postgres".

The new database has no tables yet, and \dt says so:

stationery=#Plain text
\dt
OutputPlain text
Did not find any tables.

Step 2: Create Two Tables

stationery=#SQL
CREATE TABLE pens (color text);
OutputPlain text
CREATE TABLE
stationery=#SQL
CREATE TABLE notebooks (pages integer);
OutputPlain text
CREATE TABLE

Step 3: List the Tables

stationery=#Plain text
\dt
OutputPlain text
            List of tables
 Schema |   Name    | Type  |  Owner
--------+-----------+-------+----------
 public | notebooks | table | postgres
 public | pens      | table | postgres
(2 rows)

One row for each table: its schema, name, type and owner, sorted by name.

Step 4: See Each Table’s Size

stationery=#Plain text
\dt+
OutputPlain text
                                         List of tables
 Schema |   Name    | Type  |  Owner   | Persistence | Access method |    Size    | Description
--------+-----------+-------+----------+-------------+---------------+------------+-------------
 public | notebooks | table | postgres | permanent   | heap          | 0 bytes    |
 public | pens      | table | postgres | permanent   | heap          | 8192 bytes |
(2 rows)

\dt+ adds four columns: Persistence (permanent for a normal table), Access method, Size on disk and Description. pens takes 8192 bytes while still empty: a table with a text column comes with a TOAST table, where PostgreSQL keeps long values, and the size counts it.

Step 5: List Only Some Tables

stationery=#Plain text
\dt n*
OutputPlain text
            List of tables
 Schema |   Name    | Type  |  Owner
--------+-----------+-------+----------
 public | notebooks | table | postgres
(1 row)

A pattern lists only the tables whose names match it. * stands for any characters, so n* means every name that starts with n.

Step 6: List Tables with SQL

\dt works only in psql. In other tools, such as pgAdmin’s Query Tool, ask the information_schema.tables view instead:

stationery=#SQL
SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';
OutputPlain text
 table_name
------------
 pens
 notebooks
(2 rows)

The same two tables. Without an ORDER BY, rows can come back in any order, so the list isn’t sorted.

Clean Up

To remove the example, connect to another database and drop the test database:

stationery=#Plain text
\c postgres
OutputPlain text
You are now connected to database "postgres" as user "postgres".
postgres=#SQL
DROP DATABASE stationery;
OutputPlain text
DROP DATABASE

Warning: DROP DATABASE deletes the database and everything in it, and it can’t be undone.

Common Mistakes

MistakeWhat happensDo this instead
Typing \dt in pgAdmin’s Query ToolERROR: syntax error at or near "\": the Query Tool runs SQL, and \dt is a psql command.Use the query from Step 6, or open Schemas › public › Tables in pgAdmin’s Object Explorer.
Running \dt while connected to another databaseDid not find any tables., or that database’s tables instead of yours.Switch first, with \c and the database’s name.

FAQ

Why doesn’t \dt show a table I know is there?

Without a pattern, \dt lists the tables in the schemas on your search path, usually only public. For a table in another schema, give the schema in a pattern, such as \dt sales.*. How to Create a Schema in PostgreSQL and How to Set the search_path in PostgreSQL explain schemas and the search path.

What does Owner mean?

The role that owns the table, usually the user who created it. The owner can change or drop the table.

Conclusion

\dt lists the tables in the database you’re connected to, \dt+ adds their sizes, and a pattern narrows the list. In tools without psql’s commands, query information_schema.tables instead.

Additional Resources

Postgres, PostgreSQL and the Slonik Logo are trademarks or registered trademarks of the PostgreSQL Community Association of Canada, and used with their permission.

0 comments