【问题标题】:postgres count tables in db数据库中的postgres计数表
【发布时间】:2017-04-09 23:57:45
【问题描述】:

我想获取 pgadmin 中“hgp17290_data”数据库中的表数。 我一直在搞乱 POSTGRES SQL,我可以得到一个 db 的大小,如下所示:

select pg_database_size('hgp17290_data');

但我无法获取此数据库中的表数,以下示例我无法使用

SELECT
    pg_database.datname,
    pg_size_pretty(pg_database_size(pg_database.datname)) AS size
    FROM pg_database;

SELECT pg_size_pretty( pg_total_relation_size('DemoLayer1'));

select count(*)
from information_schema.tables
where table_schema = 'hgp17290_data';

select * from  pg_stat_user_tables ;
select count(*) from  'GeoAppBuilderData' ; 
select * from  pg_stat_all_tables ;

SELECT count(*) FROM GeoAppBuilderData.tables WHERE table_schema NOT IN ('GeoAppBuilderData', 'pg_catalog');

select pg_database_count('hgp17290_data');

select count(1) from ('hgp17290_data');

select 'hgp17290_data' db, 'users' 'hgp17290_data', count[1] "rowscount" from hgp17290_data.users



select table_schema, 
       table_name, 
       (xpath('/row/cnt/text()', xml_count))[1]::text::int as row_count
from (
  select table_name, table_schema, 
         query_to_xml(format('select count(*) as cnt from %I.%I', table_schema, table_name), false, true, '') as xml_count
  from information_schema.tables
  where table_schema = 'hgp17290_data' --<< change here for the schema you want

  SELECT COUNT(*) FROM ('hgp17290_data');

  SELECT 'hgp17290_data' AS table_name, COUNT(*) FROM table_1
) 

【问题讨论】:

  • 问题是什么?从information_schema.tables 开始计数似乎是最简单的方法。
  • select count(*) from pg_tables

标签: sql postgresql postgresql-9.1


【解决方案1】:

你已经尝试过这个查询:

select count(*)
from information_schema.tables
where table_schema = 'hgp17290_data';

但在我看来,您不必在此处提供架构名称等于数据库名称的 where 子句(hgp17290_data 是您的数据库名称,而不是某些架构名称)。尝试简单如下:

select count(*)
from information_schema.tables;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-08
    • 1970-01-01
    • 1970-01-01
    • 2011-02-19
    • 2011-08-12
    • 1970-01-01
    • 2012-08-21
    相关资源
    最近更新 更多