PostgreSQL exposes information about views through pg_catalog.pg_views and information_schema.views. These are system views, not ordinary tables. You can use them to list views, find their owners and inspect the tables they reference.
The examples below are read-only queries. Replace example names with exact names from the intended database. Straight single quotes delimit text values; the typographic quotes in parts of the original article have been corrected.
Count views
SELECT count(*) FROM pg_catalog.pg_views;
SELECT count(*) FROM information_schema.views;
These counts need not be equal. The information-schema view is limited to views accessible to the current user. See information_schema.views.
List views in ascending order
SELECT viewname
FROM pg_catalog.pg_views
ORDER BY viewname ASC;
Filter by schema
SELECT viewname
FROM pg_catalog.pg_views
WHERE schemaname = 'your_schema'
ORDER BY viewname ASC;
Filter by owner
SELECT viewname
FROM pg_catalog.pg_views
WHERE viewowner = 'your_role'
ORDER BY viewname ASC;
The column names and meanings are documented in pg_views.
List views and referenced tables
A view does not necessarily belong to one table: its query may reference several. Including both schemas helps distinguish objects with the same name.
SELECT vtu.view_schema, vtu.view_name AS viewname,
vtu.table_schema, vtu.table_name AS tablename
FROM information_schema.view_table_usage AS vtu
JOIN information_schema.views AS v
ON vtu.view_catalog = v.table_catalog
AND vtu.view_schema = v.table_schema
AND vtu.view_name = v.table_name
ORDER BY vtu.view_schema, vtu.view_name, vtu.table_schema, vtu.table_name;
Find tables referenced by one view
SELECT vtu.view_name AS viewname,
vtu.table_schema, vtu.table_name AS tablename
FROM information_schema.view_table_usage AS vtu
JOIN information_schema.views AS v
ON vtu.view_catalog = v.table_catalog
AND vtu.view_schema = v.table_schema
AND vtu.view_name = v.table_name
WHERE vtu.view_schema = 'your_schema'
AND vtu.view_name = 'your_view'
ORDER BY vtu.table_schema, vtu.table_name;
view_table_usage has visibility limits: referenced tables must be owned by a currently enabled role, and system tables are not included. An empty result is not proof that a view has no dependencies.
This English edition corrects the quotes, calls the catalogue objects views, and qualifies the original one-view/one-table wording. No queries were run against your database.
Comments (0)
Comments are shown in their original language.
No comments have been published yet. Be the first to join the conversation.