Is there any way by which I can get the total number of tables in a Postgresql database? The postgresql version I'm using is PostgreSQL 8.4.14.
Asked
Active
Viewed 5.7k times
61
-
To explore what's going on in only one database I usually use `\d`. With this you can list the total number of tables, views and the sequences also. – omar May 05 '15 at 17:47
5 Answers
87
select count(*)
from information_schema.tables;
Or if you want to find the number of tables only for a specific schema:
select count(*)
from information_schema.tables
where table_schema = 'public';

Fellipe Sanches
- 7,395
- 4
- 32
- 33
-
What if we want to count the number of the tables ? I know we can see the number but i want to use this number in a sql statement ? – Capan Nov 07 '17 at 19:26
-
1Use `where table_type = 'BASE TABLE'` to exclude Views and Temporary Tables from the results. – Fellipe Sanches Feb 14 '23 at 18:56
21
Just try to search in pg_stat... tables or information_schema you can find there very useful informations about your database.
Example:
select * from pg_stat_user_tables ;
select count(*) from pg_stat_user_tables ;
select * from pg_stat_all_tables ;

sufleR
- 2,865
- 17
- 31
3
If you want to get just the number of tables (without views) then:
select count(*) from information_schema.tables where table_type = 'BASE TABLE';
Or you can also filter by schema name by adding the where condition like:
table_schema = 'public'

Popa Andrei
- 2,299
- 21
- 25
0
Try this, it'w work for me
select t.table_catalog, t.table_schema, t.table_name from information_schema.tables t where t.table_catalog = 'database_name';

Ari Anggoro
- 11
- 1
-
1Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 20 '23 at 22:08