List All Views in a PostgreSQL Database with SQL
How to List All Views in a PostgreSQL Database
Views are saved SQL queries stored in the database. A production database can accumulate dozens or hundreds of views over time — many created by developers, some by tools, and some that are no longer used. Knowing what views exist, which schema they belong to, and what they actually do is essential for documentation, auditing, and cleanup.
This SQL script queries information_schema.views to return every view in the current database, along with its schema and full SQL definition.
SQL Script
1SELECT
2 table_schema AS view_schema,
3 table_name AS view_name,
4 view_definition
5FROM
6 information_schema.views
7WHERE
8 table_schema NOT IN ('pg_catalog', 'information_schema')
9ORDER BY
10 table_schema,
11 table_name;
Notes: Works on any version of PostgreSQL. Returns one row per view. The view_definition column contains the full SQL text of each view as stored by PostgreSQL. System views in pg_catalog and information_schema are excluded.
Code Breakdown
information_schema.views
A standard SQL view that lists every view accessible to the current user in the current database. Columns include the schema name, the view name, and the SQL text of the view definition.
table_schema NOT IN ('pg_catalog', 'information_schema')
Excludes PostgreSQL internal views. Without this filter, you would see hundreds of system views alongside your own.
table_schema AS view_schema, table_name AS view_name
Column aliases to make the output self-explanatory. information_schema.views uses the table_schema and table_name naming convention even for views.
view_definition
The full SQL text of the view as stored in the system catalog. PostgreSQL normalises this text when storing it — the formatting may differ from what was originally written with CREATE VIEW.
ORDER BY table_schema, table_name
Groups views by schema first, then sorts alphabetically within each schema. Useful when a database has multiple application schemas.
Key Points
- Only views accessible to the current user are returned. Superusers see all views.
- The
view_definitiontext is the normalised form PostgreSQL stores internally, not the originalCREATE VIEWstatement verbatim. - Views in the
publicschema appear withview_schema = 'public'. - An empty result means the current database has no user-created views.
Insights and Explanations
Views are invisible overhead unless you actively track them. Common problems found during a view audit:
- Stale views that reference tables or columns that were dropped or renamed. These views exist in the catalog but will error when queried.
- Redundant views created by multiple developers for the same purpose under slightly different names.
- Undocumented views used by applications but never added to schema documentation or migration scripts.
- Security-sensitive views that expose more columns than intended to lower-privileged users.
To check whether a view is still valid, use:
1EXPLAIN SELECT * FROM your_schema.your_view_name;
If the view references a missing table or column, EXPLAIN will return an error and identify the problem.
To get a more readable view definition, use pg_get_viewdef():
1SELECT
2 schemaname,
3 viewname,
4 pg_get_viewdef(schemaname || '.' || viewname, true) AS definition
5FROM
6 pg_views
7WHERE
8 schemaname NOT IN ('pg_catalog', 'information_schema')
9ORDER BY
10 schemaname,
11 viewname;
The second argument true adds line breaks to make the output more readable.
Additional Considerations
- Permissions: Any user with access to
information_schemacan run this query. Results are filtered to views the user hasSELECTprivilege on. - Materialized views: This query does not return materialized views. Those are stored in
pg_matviewsand are not part ofinformation_schema.views. Querypg_matviewsseparately if needed. - Dependent views: Views can depend on other views. Dropping a view that another view depends on will fail unless you use
CASCADE. Usepg_dependto map view dependencies before dropping anything.
References
Posts in this series
- How Many Connections Can Your PostgreSQL Database Handle?
- PostgreSQL Backend Connections via pg_stat_database
- Identifying Blocking PostgreSQL Queries using pg_stat_activity
- List PostgreSQL Databases by Size with Access Check
- Assess PostgreSQL Database Sizes Quickly and Easily
- Unveiling Your PostgreSQL Server - A Diagnostic Powerhouse
- Keep Your PostgreSQL Database Clean, Identify Idle Connections
- Query the PostgreSQL Configuration
- PostgreSQL Recovery Monitoring: Essential SQL Insights
- Restart All PostgreSQL Sequences with ALTER SEQUENCE
- Monitor Running Queries in PostgreSQL using pg_stat_activity
- Monitor PostgreSQL Active Sessions with pg_stat_activity
- PostgreSQL Error Handling Settings via pg_settings
- PostgreSQL File Location Settings Query via pg_settings
- PostgreSQL Lock Management Settings via pg_settings
- PostgreSQL Logging Configuration Query via pg_settings
- Monitor PostgreSQL Memory Settings with pg_settings
- PostgreSQL Table Row Count Estimates with SQL
- List PostgreSQL Tables by Size with SQL
- PostgreSQL WAL Settings Query Guide
- PostgreSQL SSL Settings Query Guide
- PostgreSQL Statistics Settings Query Guide
- PostgreSQL Resource Settings Query Guide
- PostgreSQL Replication Settings Query Guide
- PostgreSQL Query Planning Settings Query Guide
- PostgreSQL Preset Options Settings Query Guide
- PostgreSQL Miscellaneous Settings Query Guide
- Count PostgreSQL Sessions by State with SQL
- Kill Idle PostgreSQL Sessions with SQL
- Grant SELECT on All Tables in PostgreSQL
- Identify Insert-Only Tables in PostgreSQL
- Detect Soft Delete Patterns in PostgreSQL
- List PostgreSQL Object Comments with SQL
- List Foreign Key Constraints in PostgreSQL
- List PostgreSQL Enum Types and Their Values with SQL
- List All Views in a PostgreSQL Database with SQL
- Find PostgreSQL Tables Without a Primary Key
- List PostgreSQL Partitioned Tables with SQL