Quantcast
Channel: CodeFari
Viewing all articles
Browse latest Browse all 265

How to get a list column names and data-type of a table in PostgreSQL?

$
0
0
If you are a DBA and you are working on a data-dictionary for a project so that you can keep track of schema changes and so on, then this article will help you achieve your right things. Using schema information_schema and table columns we can get the all infornation of the particular column.

Get the list of columns and its details using information_schema.columns


SELECT*
  FROMinformation_schema.columns
 WHEREtable_schema='schema_name'
   ANDtable_name   ='table_name';


Note:
schema_name = public
table_name = product
Also, using the following query you can get the list of column name only without other information.

SELECT*
  FROMtable_name
 WHEREfalse;

Using pg_catalog.pg_attribute, get the list of columns

List the columns and their descriptions such as data type, column name, etc .. Use the following query for the table.


SELECT
        a.attnameas"Column",
        pg_catalog.format_type(a.atttypid,a.atttypmod)as"Datatype"
                                --more attributes
    FROM
        pg_catalog.pg_attributea
    WHERE
        a.attnum> 0
        ANDNOTa.attisdropped
        ANDa.attrelid=(
            SELECTc.oid
            FROMpg_catalog.pg_classc
                LEFTJOINpg_catalog.pg_namespacenONn.oid=c.relnamespace
            WHEREc.relname~'^(table_name)$'
                ANDpg_catalog.pg_table_is_visible(c.oid)
        );



And see the following query which may help you to list the column 


SELECT
    "pg_attribute".attnameas"Column",
    pg_catalog.format_type("pg_attribute".atttypid,"pg_attribute".atttypmod)as"Datatype",
    not("pg_attribute".attnotnull)AS"Nullable"
                --,more attributes
FROM
    pg_catalog.pg_attribute"pg_attribute"
WHERE
    "pg_attribute".attnum> 0
    ANDNOT"pg_attribute".attisdropped
    AND"pg_attribute".attrelid=(
        SELECT"pg_class".oid
        FROMpg_catalog.pg_class"pg_class"
            LEFTJOINpg_catalog.pg_namespace"pg_namespace"ON"pg_namespace".oid="pg_class".relnamespace
        WHERE
            "pg_namespace".nspname='schema_name'
            AND"pg_class".relname='table_name'
    );


Creating function to get column list of particular table.

Using above query we can create a function with schema and table name parameter, one thing also I want to improve like I want to top one value of a column for the data illustration, basically we use it in data-dictionary.

CREATEORREPLACEFUNCTIONget_column_list(my_schema_nametext,my_table_nametext)
   RETURNSTABLE (
                table_schematext,
                table_nametext,
                column_nametext,
                data_typetext,
                is_nullabletext, 
                valuetext
)
AS $$
DECLAREcntinteger;
mytabletext;
valtext;
coltext;
BEGIN

CREATEtemporaryTABLETmp
(
                idINTGENERATEDALWAYSASIDENTITY,
                table_schematext,
                table_nametext,
                column_nametext,
                data_typetext,
                is_nullabletext, 
                valuetext,
    PRIMARYKEY(id)
);
 
INSERTINTOTmp(  table_schema,table_name,column_name,data_type,is_nullable)
SELECTc.table_schema,c.table_name,c.column_name,c.data_type,c.is_nullable
  FROMinformation_schema.columnsc
WHEREc.table_schema=my_schema_name
  ANDc.table_name   =my_table_name;

SELECT  COUNT(*)INTOcntFROMTmp;

LOOP
EXITWHEN  cnt=0;
 col:=(select  t.column_name  fromTmptwheret.id=cnt);
 mytable:=(selectt.table_name  fromTmptwheret.id=cnt);  
EXECUTEformat('update Tmp t set value= (SELECT %s from %s limit 1) where t.id=%s;',col,mytable,cnt);
cnt:=cnt-1;
ENDLOOP; 
                RETURNquery
                SELECTt.table_schema,t.table_name,t.column_name,t.data_type,t.is_nullable,t.valuefromTmpt;
DROPTABLE  ifexistsTmp;
END; $$ 
LANGUAGE'plpgsql';


Suppose we have the Products table as below, and want to the list of the columns and its information.

CREATETABLEproducts
(
    product_idserialNOTNULL,
    product_nametext,
    categorytext,
    quantitynumeric
);

-- Inserted some records into PRODUCTS table

INSERTINTOproducts(product_name,category,quantity)
VALUES('prod_1','category_1',100);

INSERTINTOproducts(product_name,category,quantity)
VALUES('prod_2','category_2',200);

INSERTINTOproducts(product_name,category,quantity)
VALUES('prod_3','category_3',400);

INSERTINTOproducts(product_name,category,quantity)
VALUES('prod_4','category_4',500);


  
Get the column list using get_column_list() funtion.

SELECT*FROM   get_column_list('public','products');


Result:


Viewing all articles
Browse latest Browse all 265

Trending Articles