PostgreSQL List Views
Summary: in this tutorial, you will learn how to list views in the PostgreSQL database server using the psql
command and SQL statements.
In PostgreSQL, views are named queries stored directly within the database server. These views allow you to encapsulate complex SQL queries, enabling you to retrieve specific subset data from underlying tables easily.
PostgreSQL offers some options for listing views within the current database. You can either use the \dv
command in psql
or query the information_schema.views
and pg_matviews
views directly.
Listing views using psql
First, open the Command Prompt on Windows or Terminal on Unix-like systems and connect to the PostgreSQL using the following psql
command:
It’ll prompt you to enter a password for the postgres
user.
Second, change the current database to the desired one where you want to list the views, for example, dvdrental
:
Finally, list the view in the database using the \dv
command:
In this command, dv
stands for display views. The \dv
command allows you to quickly examine the views in the database without having to write SQL queries.
Output:
The output has four columns:
Schema
: Indicates the schema of the view. When you create a view without a schema, it defaults to public.Name
: Specifies the name of the view.Type
: Denotes the type of the object, which isview
in this case.Owner
: Shows the user account that created the view.
Listing view using SQL statement
PostgreSQL offers various database views that contain information about objects defined in the current database through the information schema.
To retrieve the information about database views, you can execute the following SQL statement:
The output will display the schema and the name of the views:
Listing materialized views
To retrieve all materialized views, you can query them from the pg_matviews
view:
Output:
The output includes detailed information about materialized views, including their definitions.
If you solely want to get the names of the materialized view, you can use the following query:
This query will return only the names of the materialized views.
Output:
Summary
- Utilize the
\dv
command to list all views of a database using thepsql
program. - Use the
information_schemas.views
view to retrieve information about views. - Use the
pg_matviews
view to obtain the materialized views.