In PostgreSQL, we can use the information_schema.views
view to return all views in a database.
We can also use the \dv
psql command to do the same thing.
Example of the information_schema.views
View
Here’s an example of using the the information_schema.views
view to return a list of views in the current database:
SELECT
table_schema AS schema,
table_name AS view
FROM
information_schema.views
WHERE
table_schema NOT IN ('information_schema', 'pg_catalog')
ORDER BY
schema ASC,
view ASC;
Example result:
+--------+----------------------------+ | schema | view | +--------+----------------------------+ | public | actor_info | | public | customer_list | | public | film_list | | public | nicer_but_slower_film_list | | public | sales_by_film_category | | public | sales_by_store | | public | staff_list | +--------+----------------------------+
Here, we excluded information_schema
and pg_catalog
so that we only get public and user created views, and we don’t get bombarded with system views.
In this example, I was in the pagila
sample database.
Example of the \dv
Command
When using psql, we can alternatively use the \dv
command:
\dv
Example result:
List of relations +--------+----------------------------+------+----------+ | Schema | Name | Type | Owner | +--------+----------------------------+------+----------+ | public | actor_info | view | postgres | | public | customer_list | view | postgres | | public | film_list | view | postgres | | public | nicer_but_slower_film_list | view | postgres | | public | sales_by_film_category | view | postgres | | public | sales_by_store | view | postgres | | public | staff_list | view | postgres | +--------+----------------------------+------+----------+ (7 rows)