Site icon JD Bots

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

Syntax

\dt [ pattern ]
\dt+ [ pattern ]

Syntax

SELECT table_name FROM information_schema.tables
WHERE table_schema = 'schema_name';

See psql (the \d commands) and tables 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=#

CREATE DATABASE stationery;

Output

CREATE DATABASE

postgres=#

\c stationery

Output

You are now connected to database "stationery" as user "postgres".

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

stationery=#

\dt

Output

Did not find any tables.

Step 2: Create Two Tables

stationery=#

CREATE TABLE pens (color text);

Output

CREATE TABLE

stationery=#

CREATE TABLE notebooks (pages integer);

Output

CREATE TABLE

Step 3: List the Tables

stationery=#

\dt

Output

            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=#

\dt+

Output

                                         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=#

\dt n*

Output

            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=#

SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';

Output

 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=#

\c postgres

Output

You are now connected to database "postgres" as user "postgres".

postgres=#

DROP DATABASE stationery;

Output

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

Related

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

Exit mobile version