Select datatype of the field in postgres

后端 未结 7 2075

How do I get datatype of specific field from table in postgres ? For example I have the following table, student_details ( stu_id integer, stu_name varcha

7条回答
  •  醉话见心
    2020-12-12 12:04

    Pulling data type from information_schema is possible, but not convenient (requires joining several columns with a case statement). Alternatively one can use format_type built-in function to do that, but it works on internal type identifiers that are visible in pg_attribute but not in information_schema. Example

    SELECT a.attname as column_name, format_type(a.atttypid, a.atttypmod) AS data_type
    FROM pg_attribute a JOIN pg_class b ON a.attrelid = b.relfilenode
    WHERE a.attnum > 0 -- hide internal columns
    AND NOT a.attisdropped -- hide deleted columns
    AND b.oid = 'my_table'::regclass::oid; -- example way to find pg_class entry for a table
    

    Based on https://gis.stackexchange.com/a/97834.

提交回复
热议问题