# Schema name, table name and row count

My database has 50+ schemas. Each schema has around 100 tables and some views.

I want a query that tells me the schema name, table name, and total number of rows in each table. The below query:

``````select table_schema, table_name, table_type, count(*)
from information_schema.tables
group by table_schema, table_name, table_type
``````

...gives the total number of tables with each name, rather than giving the row count. I am using pgAdmin 3 version 1.10

• Normally you get the number of rows in a table by querying the table itself: `SELECT COUNT(*) FROM tablename`. Unless PostgreSQL stores row counts externally somewhere, you will probably need to use dynamic SQL: generate a `SELECT COUNT(*) FROM...` query for every table of every schema listed in `information_schema.tables` and then execute the resulting script. – Andriy M Mar 25 '16 at 14:30

## 1 Answer

This should do the trick:

``````select nspname, relname, relkind, reltuples from pg_class c
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
where relkind in ('r', 'v');
``````

Note that the reltuples is an estimate of the rows not the actual count at the moment but generally it's quite accurate (depends on how up to date your statistics are).

• Hi. Thanks for proposing a solution but this is not giving the accurate count. – romil gaurav Mar 28 '16 at 9:48
{"ft_lang_label":"__label__en","ft_lang_prob":0.7524654,"math_prob":0.75343376,"size":755,"snap":"2021-04-2021-17","text_gpt3_token_len":173,"char_repetition_ratio":0.14647137,"word_repetition_ratio":0.0,"special_character_ratio":0.2397351,"punctuation_ratio":0.18367347,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96037525,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-04-18T18:30:16Z\",\"WARC-Record-ID\":\"<urn:uuid:130e51f0-1bfd-4d1f-b664-be8283a0a515>\",\"Content-Length\":\"162925\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:65e41afd-5344-4e85-ba96-d858e131d0bb>\",\"WARC-Concurrent-To\":\"<urn:uuid:8b00e0aa-2b86-429e-9b61-9dffdee8757c>\",\"WARC-IP-Address\":\"151.101.65.69\",\"WARC-Target-URI\":\"https://dba.stackexchange.com/questions/133378/schema-name-table-name-and-row-count\",\"WARC-Payload-Digest\":\"sha1:QNROTDMSH72XOIW3DJM3IBQ2CJ3RF4QX\",\"WARC-Block-Digest\":\"sha1:KKJ4J4WFE7EP3EELECFBKWSEELJNVUH3\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-17/CC-MAIN-2021-17_segments_1618038507477.62_warc_CC-MAIN-20210418163541-20210418193541-00407.warc.gz\"}"}