SELECTf.attnum AS number,f.attname AS name,f.attnum,f.attnotnull AS notnull,pg_catalog.format_type(f.atttypid,f.atttypmod) AS type,CASEWHEN p.contype = 'p' THEN 't'ELSE 'f'END AS primarykey,CASEWHEN p.contype = 'u' THEN 't'ELSE 'f'END AS uniquekey,CASEWHEN p.contype = 'f' THEN g.relnameEND AS foreignkey,CASEWHEN p.contype = 'f' THEN p.confkeyEND AS foreignkey_fieldnum,CASEWHEN p.contype = 'f' THEN g.relnameEND AS foreignkey,CASEWHEN p.contype = 'f' THEN p.conkeyEND AS foreignkey_connnum,CASEWHEN f.atthasdef = 't' THEN d.adsrcEND AS defaultFROM pg_attribute fJOIN pg_class c ON c.oid = f.attrelidJOIN pg_type t ON t.oid = f.atttypidLEFT JOIN pg_attrdef d ON d.adrelid = c.oid AND d.adnum = f.attnumLEFT JOIN pg_namespace n ON n.oid = c.relnamespaceLEFT JOIN pg_constraint p ON p.conrelid = c.oid AND f.attnum = ANY (p.conkey)LEFT JOIN pg_class AS g ON p.confrelid = g.oidWHERE c.relkind = 'r'::charAND n.nspname = '%s' -- Replace with Schema nameAND c.relname = '%s' -- Replace with table nameAND f.attnum > 0 ORDER BY number;
Use this command
\d table name
like
\d queuerecords
Table "public.queuerecords"Column | Type | Modifiers-----------+-----------------------------+-----------id | uuid | not nullendtime | timestamp without time zone |payload | text |queueid | text |starttime | timestamp without time zone |status | text |
SELECTa.attname AS Field,t.typname || '(' || a.atttypmod || ')' AS Type,CASE WHEN a.attnotnull = 't' THEN 'YES' ELSE 'NO' END AS Null,CASE WHEN r.contype = 'p' THEN 'PRI' ELSE '' END AS Key,(SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid), '\'(.*)\'')FROMpg_catalog.pg_attrdef dWHEREd.adrelid = a.attrelidAND d.adnum = a.attnumAND a.atthasdef) AS Default,'' as ExtrasFROMpg_class cJOIN pg_attribute a ON a.attrelid = c.oidJOIN pg_type t ON a.atttypid = t.oidLEFT JOIN pg_catalog.pg_constraint r ON c.oid = r.conrelidAND r.conname = a.attnameWHEREc.relname = 'tablename'AND a.attnum > 0
ORDER BY a.attnum
SELECTn.nspname as schema,c.relname as table,f.attname as column,f.attnum as column_id,f.attnotnull as not_null,f.attislocal not_inherited,f.attinhcount inheritance_count,pg_catalog.format_type(f.atttypid,f.atttypmod) AS data_type_full,t.typname AS data_type_name,CASEWHEN f.atttypmod >= 0 AND t.typname <> 'numeric'THEN (f.atttypmod - 4) --first 4 bytes are for storing actual length of dataEND AS data_type_length,CASEWHEN t.typname = 'numeric' THEN (((f.atttypmod - 4) >> 16) & 65535)END AS numeric_precision,CASEWHEN t.typname = 'numeric' THEN ((f.atttypmod - 4)& 65535 )END AS numeric_scale,CASEWHEN p.contype = 'p' THEN 't'ELSE 'f'END AS is_primary_key,CASEWHEN p.contype = 'p' THEN p.connameEND AS primary_key_name,CASEWHEN p.contype = 'u' THEN 't'ELSE 'f'END AS is_unique_key,CASEWHEN p.contype = 'u' THEN p.connameEND AS unique_key_name,CASEWHEN p.contype = 'f' THEN 't'ELSE 'f'END AS is_foreign_key,CASEWHEN p.contype = 'f' THEN p.connameEND AS foreignkey_name,CASEWHEN p.contype = 'f' THEN p.confkeyEND AS foreign_key_columnid,CASEWHEN p.contype = 'f' THEN g.relnameEND AS foreign_key_table,CASEWHEN p.contype = 'f' THEN p.conkeyEND AS foreign_key_local_column_id,CASEWHEN f.atthasdef = 't' THEN d.adsrcEND AS default_valueFROM pg_attribute fJOIN pg_class c ON c.oid = f.attrelidJOIN pg_type t ON t.oid = f.atttypidLEFT JOIN pg_attrdef d ON d.adrelid = c.oid AND d.adnum = f.attnumLEFT JOIN pg_namespace n ON n.oid = c.relnamespaceLEFT JOIN pg_constraint p ON p.conrelid = c.oid AND f.attnum = ANY (p.conkey)LEFT JOIN pg_class AS g ON p.confrelid = g.oidWHERE c.relkind = 'r'::charAND f.attisdropped = falseAND n.nspname = '%s' -- Replace with Schema nameAND c.relname = '%s' -- Replace with table nameAND f.attnum > 0ORDER BY f.attnum;