postgresql查询一个库有多少表

查询当前数据库用户表总数可用:SELECT count() AS table_count FROM information_schema.tables WHERE table_schema NOT IN ('information_schema', 'pg_catalog') AND table_type = 'BASE TABLE'; 若要统计指定模式如public下的表,可使用:SELECT count() AS table_count FROM information_schema.tables WHERE table_schema = 'public' AND table_type = 'BASE TABLE'; 也可通过pg_tables视图简化查询:SELECT count() AS table_count FROM pg_tables WHERE schemaname NOT IN ('information_schema', 'pg_catalog'); 或仅查public模式:SELECT count() AS table_count FROM pg_tables WHERE schemaname = 'public'; 推荐使用information_schema.tables或pg_tables,兼容性好且简洁清晰。

要查询一个 PostgreSQL 数据库中有多少张表,可以使用以下 SQL 语句:

查询当前数据库中所有用户表的数量

执行下面的查询,统计 当前数据库中所有用户创建的表(不包含系统表):

SELECT count(*) AS table_count
FROM information_schema.tables
WHERE table_schema NOT IN ('information_schema', 'pg_catalog')
  AND table_type = 'BASE TABLE';

这个查询会返回一个数字,表示当前数据库中用户表的总数。

只查指定模式(如 public)下的表数量

如果你只想统计某个模式(例如 public)下的表,可以加上 table_schema = 'public' 条件:

SELECT count(*) AS table_count
FROM information_schema.tables
WHERE table_schema = 'public'
  AND table_type = 'BASE TABLE';

通过 pg_tables 查看(更简洁方式)

pg_tables 是 PostgreSQL 提供的一个系统视图,专门用于查看表信息。用它也可以快速统计:

SELECT count(*) AS table_count
FROM pg_tables
WHERE schemaname NOT IN ('information_schema', 'pg_catalog');

或者只查 public 模式:

SELECT count(*) AS table_count
FROM pg_tables
WHERE schemaname = 'public';

基本上就这些常用方法。推荐使用 information_schema.tablespg_tables,它们清晰且兼容性好。