How to get a list column names and datatype of a table in PostgreSQL?

前端 未结 11 2244
[愿得一人]
[愿得一人] 2020-12-02 10:02

With the following query, we can get a list of column names and datatype of a table in PostgreSQL.

11条回答
  •  被撕碎了的回忆
    2020-12-02 10:42

    To make this topic 'more complete'.

    I required the column names and data types on a SELECT statement (not a table).

    If you want to do this on a SELECT statement instead of an actual existing table, you can do the following:

    DROP TABLE IF EXISTS abc;
    CREATE TEMPORARY TABLE abc AS
    -- your select statement here!
    SELECT 
        *
    FROM foo
    -- end your select statement
    ;
    
    select column_name, data_type 
    from information_schema.columns 
    where table_name = 'abc';
    DROP IF EXISTS abc;
    

    Short explanation, it makes a (temp) table of your select statement, which you can 'call' upon via the query provided by (among others) @a_horse_with_no_name and @selva.

    Hope this helps.

提交回复
热议问题