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

前端 未结 15 2268
无人及你
无人及你 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:12

    I like Daniel Vérité's answer. But when you can't use a CREATE statement you can either use a bash solution or, if you're a windows user, a powershell one:

    # You don't need this if you have pgpass.conf
    $env:PGPASSWORD = "userpass"
    
    # Get table list
    $tables = & 'C:\Program Files\PostgreSQL\9.4\bin\psql.exe' -U user -w -d dbname -At -c "select table_name from information_schema.tables where table_type='BASE TABLE' AND table_schema='schema1'"
    
    foreach ($table in $tables) {
        & 'C:\path_to_postresql\bin\psql.exe' -U root -w -d dbname -At -c "select '$table', count(*) from $table"
    }
    

提交回复
热议问题