How do you find the row count for all your tables in Postgres

前端 未结 15 2324
无人及你
无人及你 2020-11-22 12:31

I\'m looking for a way to find the row count for all my tables in Postgres. I know I can do this one table at a time with:

SELECT count(*) FROM table_name;
         


        
15条回答
  •  我在风中等你
    2020-11-22 13:22

    You Can use this query to generate all tablenames with their counts

    select ' select  '''|| tablename  ||''', count(*) from ' || tablename ||' 
    union' from pg_tables where schemaname='public'; 
    

    the result from the above query will be

    select  'dim_date', count(*) from dim_date union 
    select  'dim_store', count(*) from dim_store union
    select  'dim_product', count(*) from dim_product union
    select  'dim_employee', count(*) from dim_employee union
    

    You'll need to remove the last union and add the semicolon at the end !!

    select  'dim_date', count(*) from dim_date union 
    select  'dim_store', count(*) from dim_store union
    select  'dim_product', count(*) from dim_product union
    select  'dim_employee', count(*) from dim_employee  **;**
    

    RUN !!!

提交回复
热议问题