PostgreSQLSQLRDBMS
Last updated at 2023-09-08

2 Ways to List All Tables in psql for Postgres Database

ClickUp
Last Edit By
Last edited time
Sep 8, 2023 02:53 AM
Metatag
Slug
psql-list-all-tables
AI Status
50%
Published
Published
Date
Sep 8, 2023
Category
PostgreSQL
SQL
RDBMS
When you use pqsl to manage your Postgres databaes, you may need to list all tables that you will work on.
In this tutorial, you will learn how to use \dt and \dt+ commands to list all tables in your Postgres database.

What is psql for Postgres?

It’s okay to not know everything. Especially if you are a beginner. This applies to Postgres psql command line.
psql stands for PostgreSQL Command Line Utility.
It is a command-line tool that allows you to interact with PostgreSQL databases directly from the command line interface.
It provides a convenient way to execute SQL queries, manage database users and permissions, and perform various administrative tasks related to PostgreSQL databases.
With psql, you can connect to a PostgreSQL database and interact with it using SQL commands and statements.
You can perform tasks such as creating tables, inserting data, updating records, querying data, and more.
Additionally, psql provides features like command history, tab completion, and formatting options to make your interactions with the database more efficient.

List Tables using \dt command

The first command that you can use to list your Postgres tables is the \dt command.
Type and run this command in your pqsl session.
\dt
It will show a list of tables with minimal information.
List of relations Schema | Name | Type | Owner --------+------------+-------+--------- public | authors | table | default public | categories | table | default public | comments | table | default public | editors | table | default public | favorites | table | default public | posts | table | default public | users | table | default (7 rows)
The output is pretty well printed in table form, which makes it easy to read.
Now it is minimal; if you need more information, you can go to the next command. The \dt+ command.

List Tables with Detail using \dt+ command

This command is similar to \dt but it will show detailed information along with the list of table results.
Type and run this command in your psql session.
\dt+
It will print the result like this. A detailed information about your tables.
List of relations Schema | Name | Type | Owner | Persistence | Access method | Size | Description --------+------------+-------+---------+-------------+---------------+------------+------------- public | authors | table | default | permanent | heap | 8192 bytes | public | categories | table | default | permanent | heap | 0 bytes | public | comments | table | default | permanent | heap | 8192 bytes | public | editors | table | default | permanent | heap | 0 bytes | public | favorites | table | default | permanent | heap | 0 bytes | public | posts | table | default | permanent | heap | 8192 bytes | public | users | table | default | permanent | heap | 0 bytes | (7 rows)
As you can see, it can show you the size of each table.
This is useful for understanding the storage consumption of your database and identifying tables that might be taking up a significant amount of space.
By knowing the size of individual tables, you can optimize your database by focusing on areas that might need attention, such as archiving old data or optimizing data storage strategies.
This information allows you to make informed decisions to manage your database's performance and storage efficiently.

Conclusion

Showing tables using psql requires little effort on your side. In this tutorial you have learned that you can show tables with minimal information using \dt command and detailed information using \dt+ command.
What do you want to learn next using psql?

Discussion (0)

Related Posts