postgresql 查询所有库表
时间: 2024-01-12 21:04:42 浏览: 142
PostgreSQL中使用dblink实现跨库查询的方法
5星 · 资源好评率100%
以下是在PostgreSQL中查询所有库表的方法:
```sql
-- 查询当前库下所有表英文名称、注释信息
SELECT relname, obj_description(relfilenode, 'pg_class') AS table_comment
FROM pg_class
WHERE relkind = 'r' AND relname NOT LIKE 'pg_%' AND relname NOT LIKE 'sql_%'
ORDER BY relname;
-- 查询当前库下所有表字段信息
SELECT a.attname AS column_name,
c.relname AS table_name,
pg_catalog.format_type(a.atttypid, a.atttypmod) AS data_type,
a.attlen AS character_maximum_length,
a.atttypmod AS modifier,
a.attnotnull AS is_nullable,
col_description(a.attrelid, a.attnum) AS column_comment,
(SELECT 't' FROM pg_index WHERE c.oid = pg_index.indrelid AND a.attnum = ANY (pg_index.indkey) AND indisprimary = 't') AS is_primary_key,
(SELECT 't' FROM pg_attribute WHERE attrelid = c.oid AND attisdropped = false AND attnum = a.attnum AND attnum > 0 AND attstattarget < 0) AS is_auto_increment
FROM pg_attribute a
JOIN pg_class c ON a.attrelid = c.oid
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE a.attnum > 0 AND NOT a.attisdropped AND c.relkind = 'r' AND n.nspname = 'public'
ORDER BY c.relname, a.attnum;
```
其中,第一个查询语句可以查询当前库下所有表的英文名称和注释信息,第二个查询语句可以查询当前库下所有表的字段信息,包括字段名称、所属表、数据类型、长度、是否自增、是否主键、是否为空、注释信息等。
阅读全文