PostgreSQL的表通常都是創建在public這個schema下的,假如如今有個數據表t_student
,能夠用如下幾種方式來查詢表結構和索引信息。sql
在cmd界面使用psql鏈接db後,輸入\d加上表名便可:數據庫
\d t_student
select col.table_schema, col.table_name, col.ordinal_position, col.column_name, col.data_type, col.character_maximum_length, col.numeric_precision, col.numeric_scale, col.is_nullable, col.column_default, des.description from information_schema.columns col left join pg_description des on col.table_name::regclass = des.objoid and col.ordinal_position = des.objsubid where table_schema = 'public' and table_name = 't_student' order by ordinal_position;
或者簡單點:工具
select * from information_schema.columns where table_schema='public' and table_name='t_student';
select A.SCHEMANAME, A.TABLENAME, A.INDEXNAME, A.TABLESPACE, A.INDEXDEF, B.AMNAME, C.INDEXRELID, C.INDNATTS, C.INDISUNIQUE, C.INDISPRIMARY, C.INDISCLUSTERED, D.DESCRIPTION from PG_AM B left join PG_CLASS F on B.OID = F.RELAM left join PG_STAT_ALL_INDEXES E on F.OID = E.INDEXRELID left join PG_INDEX C on E.INDEXRELID = C.INDEXRELID left outer join PG_DESCRIPTION D on C.INDEXRELID = D.OBJOID, PG_INDEXES A where A.SCHEMANAME = E.SCHEMANAME and A.TABLENAME = E.RELNAME and A.INDEXNAME = E.INDEXRELNAME and E.SCHEMANAME = 'public' and E.RELNAME = 't_student';
select n.nspname, relname from pg_class c, pg_namespace n where c.relnamespace = n.oid and nspname = 'public' and relkind = 'r' order by relname;
對於上述的sql語句只須要修改要查詢的table name,能夠根據須要自行修改想要查詢的column。若是是經過DBeaver來鏈接數據庫,還能夠直接在當前的數據庫實例下打開schema裏的public選項,接着選中table,選中你想查看的表,能夠很直觀地看到該表的各類信息:column、index等等。spa