Postgres has dedicated System Catalog Information Functions to help with that.

To get the full view definition:

SELECT pg_get_viewdef('public.view_name');

Schema-qualification is optional. If no schema is prefixed, the current search_path setting decides visibility.

A quick hack would be to just:

SELECT * FROM public.view_name LIMIT 0;

Depending on your client, column names and types should still be displayed. Or LIMIT n to get some sample values, too. The underlying query is actually executed then (unlike with LIMIT 0).

To list columns and their data type, in order, you might base the query on pg_attribute:

SELECT attname AS column_name, format_type(atttypid, atttypmod) AS data_type
FROM   pg_attribute
WHERE  attrelid = 'public.view_name'::regclass
-- AND    NOT attisdropped
-- AND    attnum > 0
ORDER  BY attnum;

Type modifiers like maximum length are included in data_type this way.

Internally, a VIEW is implemented as special table with a rewrite rule. Details in the manual here. The table is saved in the system catalogs much like any regular table.

About the cast to regclass:

  • How to check if a table exists in a given schema

The same query works for tables or materialized views as well. Uncomment the additional filters above to only get visible user columns for tables.

Answer from Erwin Brandstetter on Stack Overflow
Top answer
1 of 2
12

Postgres has dedicated System Catalog Information Functions to help with that.

To get the full view definition:

SELECT pg_get_viewdef('public.view_name');

Schema-qualification is optional. If no schema is prefixed, the current search_path setting decides visibility.

A quick hack would be to just:

SELECT * FROM public.view_name LIMIT 0;

Depending on your client, column names and types should still be displayed. Or LIMIT n to get some sample values, too. The underlying query is actually executed then (unlike with LIMIT 0).

To list columns and their data type, in order, you might base the query on pg_attribute:

SELECT attname AS column_name, format_type(atttypid, atttypmod) AS data_type
FROM   pg_attribute
WHERE  attrelid = 'public.view_name'::regclass
-- AND    NOT attisdropped
-- AND    attnum > 0
ORDER  BY attnum;

Type modifiers like maximum length are included in data_type this way.

Internally, a VIEW is implemented as special table with a rewrite rule. Details in the manual here. The table is saved in the system catalogs much like any regular table.

About the cast to regclass:

  • How to check if a table exists in a given schema

The same query works for tables or materialized views as well. Uncomment the additional filters above to only get visible user columns for tables.

2 of 2
5
SELECT
    a.attname,
    t.typname,
    a.atttypmod
FROM pg_class c
INNER JOIN pg_attribute a ON a.attrelid = c.oid
INNER JOIN pg_type t ON t.oid = a.atttypid
WHERE c.relkind = 'v'
    AND c.relname = 'put_viewname_here';

ATTENTION: Since the viewname is unique only in the schema, you might also want to add an INNER JOIN to pg_namespace and add a condition to the where-clause.


For the first version of your question:

SELECT n.nspname
FROM pg_class c
INNER JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind = 'v'
    AND c.relname = 'put_viewname_here';

ATTENTION: This might give you multiple schemas, since a viewname is only unique inside a schema and thus a viewname does not always identify one view.

🌐
Dataedo
dataedo.com › kb › query › postgresql › list-views-columns
List views columns in PostgreSQL - PostgreSQL Data Dictionary Queries
November 8, 2018 - Query below lists all columns in views in PostgreSQL database · select t.table_schema as schema_name, t.table_name as view_name, c.column_name, c.data_type, case when c.character_maximum_length is not null then c.character_maximum_length else c.numeric_precision end as max_length, is_nullable from information_schema.tables t left join information_schema.columns c on t.table_schema = c.table_schema and t.table_name = c.table_name where table_type = 'VIEW' and t.table_schema not in ('information_schema', 'pg_catalog') order by schema_name, view_name;
🌐
PostgreSQL
postgresql.org › docs › 9.1 › infoschema-view-column-usage.html
PostgreSQL: Documentation: 9.1: view_column_usage
October 22, 2020 - You may want to view the same page for the current version, or one of the other supported versions listed above instead. The view view_column_usage identifies all columns that are used in the query expression of a view (the SELECT statement ...
People also ask

Can I export data back to my business systems with Coefficient?
Yes, Coefficient supports two-way data sync. You can not only import live data from your business systems but also export updated data back to them. Supported export actions include UPDATE (modify existing records), INSERT (create new records), UPSERT (update or insert), and DELETE operations. This works with systems like Salesforce, HubSpot, QuickBooks, Snowflake, and databases.
🌐
coefficient.io
coefficient.io › home
How to Get Column Names in PostgreSQL
Does Coefficient work with both Google Sheets and Excel?
Yes, Coefficient supports both Google Sheets and Microsoft Excel. You can install Coefficient from the Google Workspace Marketplace for Sheets or from Microsoft AppSource for Excel.
🌐
coefficient.io
coefficient.io › home
How to Get Column Names in PostgreSQL
How much does Coefficient cost?
Coefficient offers 4 pricing tiers: Free Plan (3 connectors, forever free), Starter Plan ($59/user/month for individual users), Pro Plan ($99/user/month for 5 users), and custom Enterprise pricing. All paid plans include a 30-day free trial. Our Free trial let's you leverage our Pro plan at no cost so you can test the product with every feature available.
🌐
coefficient.io
coefficient.io › home
How to Get Column Names in PostgreSQL
Top answer
1 of 5
39

Information schema vs. system catalogs

We have discussed this many times. The information schema serves certain purposes. System catalogs are the actual source of all information.

The information schema provides standardized views which help with portability - mostly across major Postgres versions as portability across different RDBMS platforms typically is an illusion once your queries are sophisticated enough to look up system catalogs. Notably, Oracle still doesn't support the information schema.

Views in the information schema must jump through many hoops to achieve a format complying to the standard. This makes them slow, sometimes very slow. Compare plans and performance for these basic objects:

EXPLAIN ANALYZE SELECT * from information_schema.columns;
EXPLAIN ANALYZE SELECT * from pg_catalog.pg_attribute;

The difference is remarkable.

Your example

For your example SELECT * from tbl compare the two queries below for this simple table:

CREATE TEMP TABLE foo(
   a numeric(12,3)
 , b timestamp(0)
);

Using pg_attribute:

SELECT attname, format_type(atttypid, atttypmod) AS type
FROM   pg_attribute
WHERE  attrelid = 'foo'::regclass
AND    attnum > 0
AND    NOT attisdropped
ORDER  BY attnum;

format_type() returns the complete type with all modifiers:

attname | type
--------+-------------------------------
a       | numeric(12,3)
b       | timestamp(0) without time zone

Also note that the cast to regclass resolves the table name according to the current search_path. It raises an exception if the name is not valid. See:

  • ERROR: could not find array type for datatype information_schema.sql_identifier

Using information_schema.columns:

SELECT column_name, data_type
FROM   information_schema.columns
WHERE  table_name = 'foo'
ORDER  BY ordinal_position;

The information is standardized, but incomplete:

column_name | data_type
------------+----------------------------
a           | numeric
b           | timestamp without time zone

To get full information for the data type you need to consider all of these columns additionally:

character_maximum_length
character_octet_length
numeric_precision
numeric_precision_radix
numeric_scale
datetime_precision
interval_type
interval_precision

Related answers:

  • How to check if a table exists in a given schema
  • List all columns for a specified table

List of pros & cons

The biggest pros (IMO) in bold.

Information schema views

  • often simpler (depends)
  • slow
  • preprocessed, which may or may not suit your needs
  • selective (users only see objects they have privileges for)
  • conforming to an SQL standard (that's implemented by some of the major RDBMS)
  • mostly portable across major Postgres versions
  • do not require much specific knowledge about Postgres
  • identifiers are descriptive, long and sometimes awkward

System catalogs

  • often more complex (depends), closer to the source
  • fast
  • complete (system columns like oid included)
  • not complying to an SQL standard
  • less portable across major Postgres versions (but basics aren't going to change)
  • require more specific knowledge about Postgres
  • identifiers are terse, less descriptive but conveniently short

Arbitrary query

To get the same list of column names and types from a query, you could use a simple trick: CREATE a temporary table from the query output, then use the same techniques as above.

You can append LIMIT 0, since you do not need actual data:

CREATE TEMP TABLE tmp123 AS
SELECT 1::numeric, now()
LIMIT  0;

To get the data type of individual columns, you can also use the function pg_typeof():

SELECT pg_typeof(1);
2 of 5
4

You can use the psql command line client.

\dt will show a list of tables

\dv will show a list of views

\d [object_name] will describe the schema of the table or view

Not sure how you would describe a query though.

More info: https://manikandanmv.wordpress.com/tag/basic-psql-commands/

🌐
Stack Overflow
stackoverflow.com › questions › 39036781 › extracting-view-tables-and-columns-in-postgresql
sql - Extracting View tables and columns in PostgreSQL - Stack Overflow
June 15, 2017 - SELECT * FROM information_schema.columns WHERE table_schema = 'public' AND table_name = 'my_view' Is there a way to get list of tables and columns in a view?
🌐
Coefficient
coefficient.io › home
How to Get Column Names in PostgreSQL
December 18, 2025 - The column_name column will contain the names of all the columns in the specified table, and the data_type column will show the data type of each column. You can also use the information_schema.columns view to retrieve column names across multiple ...
Find elsewhere
🌐
PostgreSQL
postgresql.org › docs › 10 › sql-createview.html
PostgreSQL: Documentation: 10: CREATE VIEW
November 10, 2022 - If any of the tables referenced ... a recursive view. The syntax · CREATE RECURSIVE VIEW [ schema . ] view_name (column_names) AS SELECT ...;...
🌐
Stack Exchange
dba.stackexchange.com › questions › 279111 › postgres-how-to-get-column-data-types-for-a-view
postgresql - Postgres how to get column data types for a view - Database Administrators Stack Exchange
... SELECT attname AS column_name, format_type(atttypid, atttypmod) AS data_type FROM pg_attribute WHERE attrelid = 'your_schema.table_or_view_name'::regclass -- AND NOT attisdropped -- AND attnum > 0 ORDER BY attnum;
🌐
Stack Overflow
stackoverflow.com › questions › 47428353 › in-postgresql-how-do-i-view-column-names
psql - In postgreSQL how do I view column names? - Stack Overflow
for the purpose of writing more detailed queries I would like to know how to turn on headers (in SQLite this would be .headers on), and how to view the schema (in SQLite this would be .shema table_name) ... Displaying headers is the default, but you can turn it back on using the \t command in psql: postgresql.org/docs/current/static/app-psql.html
🌐
Stack Exchange
dba.stackexchange.com › questions › 130589 › get-the-name-of-the-table-column-referenced-by-a-auto-updatable-view-column-p
Get the name of the table column referenced by a (auto updatable) view column (PostgreSQL) - Database Administrators Stack Exchange
February 26, 2016 - Given the folowing view definition create or replace view projects as select id, client_id as clientId from data.projects Is it possible, knowing the view name and the view column to get the name ...
🌐
Postgresscripts
postgresscripts.com › post › list-postgresql-database-views
List All Views in a PostgreSQL Database with SQL | Postgres Scripts
May 22, 2026 - 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.
🌐
PostgreSQL
postgresql.org › docs › current › infoschema-view-column-usage.html
PostgreSQL: Documentation: 18: 35.63. view_column_usage
May 14, 2026 - The view view_column_usage identifies all columns that are used in the query expression of a view (the SELECT statement that defines the view).
🌐
PostgreSQL
postgresql.org › docs › 8.4 › queries-select-lists.html
PostgreSQL: Documentation: 8.4: Select Lists
July 24, 2014 - As shown in the previous section, the table expression in the SELECT command constructs an intermediate virtual table by possibly combining tables, views, eliminating rows, grouping, etc. This table is finally passed on to processing by the select list. The select list determines which columns of the intermediate table are actually output. The simplest kind of select list is * which emits all columns that the table expression produces. Otherwise, a select list is a comma-separated list of value expressions (as defined in Section 4.2). For instance, it could be a list of column names:
🌐
GeeksforGeeks
geeksforgeeks.org › postgresql › how-to-check-column-types-in-postgresql
How to Check Column Types in PostgreSQL? - GeeksforGeeks
July 23, 2025 - It queries the information_sch... we have several options at our disposal, including the \d command, the pg_typeof() function, and the information_schema.columns view....
🌐
w3tutorials
w3tutorials.net › blog › how-to-get-a-list-column-names-and-datatypes-of-a-table-in-postgresql
How to Get Column Names and Data Types of a Table in PostgreSQL Using a Query — w3tutorials.net
SELECT column_name, data_type, is_nullable, column_default, character_maximum_length FROM information_schema.columns WHERE table_schema = 'public' -- Replace with your schema (e.g., 'sales') AND table_name = 'users'; -- Replace with your table ...
🌐
PostgreSQL
postgresql.org › message-id › 61241.8937.qm@web34407.mail.mud.yahoo.com
PostgreSQL: Re : Column names in view
November 24, 2007 - SELECTing from the view in psql shows the column names, as does SELECTing in the query tool. There's nothing special about this view that I can see - it joins 5 tables together, and one column is calculated in the view (and given an alias also).