Summary: in this tutorial, you will learn how to list indexes from a PostgreSQL database by using either pg_indexes
view or psql
command.
PostgreSQL does not provide a command like SHOW INDEXES
to list the index information of a table or database.
However, it does provide you with access to the pg_indexes
view so that you can query the index information.
If you use the psql
program to interact with the PostgreSQL database, you can use the \d
command to view the index information for a table.
PostgreSQL List Indexes using pg_indexes View
The pg_indexes
view allows you to access useful information on each index in the PostgreSQL database.
The pg_indexes
view consists of five columns:
schemaname
: stores the name of the schema that contains tables and indexes.
tablename
: indicates the name of the table to which the index belongs.
indexname
: represents the name of the index.
tablespace
: identifies the name of the tablespace that contains indexes.
indexdef
: contains the index definition command in the form of CREATE INDEX
statement.
The following statement lists all indexes of the schema public
in the current database:
Output:
To show all the indexes of a table, you use the following statement:
For example, to list all the indexes for the customer
table, you use the following statement:
Here is the output:
If you want to get a list of indexes for tables whose names start with the letter c
, you can use the following query:
The following shows the output:
PostgreSQL List Indexes using psql command
If you use psql
to connect to a PostgreSQL database and want to list all indexes of a table, you can use the \d
psql command as follows:
The command will return all information about the table including the table’s structure, indexes, constraints, and triggers.
For example, the following statement returns detailed information about the customer
table:
The output is:
The output shows the index of the table under the Indexes section.
- Query data from the
pg_indexes
view to retrieve the index information.
- Use the
\d table_name
command to display the table information along with indexes.