/

How to List Tables in a PostgreSQL Database

How to List Tables in a PostgreSQL Database

In PostgreSQL, there are multiple ways to list tables in the current database. In this article, we will explore two methods: using the psql tool and running an SQL query.

Method 1: Using psql

To list the tables in the current database using the psql tool, follow these steps:

  1. Open the psql tool in your terminal or command prompt.
  2. Connect to your PostgreSQL database by running the following command:
1
psql -U username -d database_name

Replace username with your PostgreSQL username and database_name with the name of your database.

  1. Once connected, run the following command to list the tables in the current database:
1
\dt

This will display a list of all the tables in the current database.

Method 2: Running an SQL Query

If you prefer to run an SQL query instead of using the psql tool, you can follow these steps:

  1. Open the psql tool in your terminal or command prompt.
  2. Connect to your PostgreSQL database by running the following command:
1
psql -U username -d database_name

Replace username with your PostgreSQL username and database_name with the name of your database.

  1. Once connected, run the following SQL query to list the tables in the current database:
1
2
3
4
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public'
ORDER BY table_name;

This query retrieves the names of all tables in the “public” schema and sorts them alphabetically.

By following either of these methods, you can easily obtain a comprehensive list of tables in your PostgreSQL database.

tags: [“PostgreSQL”, “psql”, “SQL query”, “list tables”, “current database”]