Postgres offers several commands and queries to list all the views from a database, such as the “\d”, “\dv”, “\dv+” commands, “information_schema.views”, etc. Moreover, the pgAdmin can also be used to access and display all the views of any Postgres database.
This post illustrates how to list all views of a specific database using SQL Shell and pgAdmin. The content of this Postgres post is organized as follows:
- Method 1: How to List Views Using “\d” Command?
- Method 2: How to List Views Using “\dv” or “\dv+” Command?
- Method 3: How to List Views Using “information_schema.views”?
- Method 4: How to List Views Using pgAdmin?
Method 1: How to List Views Using “\d” Command?
Launch the SQL Shell, specify the login details, and use the “\d” command to get the list of available tables, views, sequences, etc.:
\d
The output shows that the “\d” command retrieves the list of relations, including views and tables.
Method 2: How to List Views Using “\dv” or “\dv+” Command?
Alternatively, you can use the “\dv” command to get the list of Postgres views. It will retrieve the list of views only(excluding tables, sequences, etc.):
\dv;
The output shows the list of available views. Execute the “\dv+” command to get the views’ list with more details like size, persistence, etc.:
\dv+;
The targeted command returns the list of views along with persistence, size, and description.
Method 3: How to List Views Using “information_schema.views”?
Use the “information_schema.views” with the “SELECT” statement to get the list of views in Postgres. You can execute this command from any interface, like psql or pgAdmin.
SELECT table_schema AS schema_name, table_name AS view_name FROM information_schema.views WHERE table_schema != 'information_schema' AND table_schema != 'pg_catalog';
The “information_schema.views” command retrieves all the views from the user-defined schemas.
Method 4: How to List Views Using pgAdmin?
You can also list all the views of a specific database using pgAdmin. For this purpose, open the pgAdmin, provide the login details, navigate to the “Databases” tab and click on the database of your choice to expand it:
Click on “Schemas” and select the schema of your choice; by default, views are stored in the “public” schema. So, click on the “public” schema and then click on the “Views” to see the available views:
Finally, you can see the list of views under the “Views” tab.
Conclusion
In PostgreSQL, the “\d”, “\dv”, “\dv+” commands, “information_schema.views”, etc., are used to list all the views from a database. pgAdmin can also be used to display all the views of a Postgres database. To get the views list with more details like size, persistence, etc., use the “\dv+” command. This blog considered various examples to demonstrate how to list views in Postgres.