267

Is there any query available to list all tables in my Postgres DB.

I tried out one query like:

SELECT table_name FROM information_schema.tables
                      WHERE table_schema='public' 

But this query returns views also.

How can i get only table names only, not views?

7 Answers 7

437

What bout this query (based on the description from manual)?

SELECT table_name
  FROM information_schema.tables
 WHERE table_schema='public'
   AND table_type='BASE TABLE';
2
  • 3
    What is a table type? Sep 8, 2018 at 23:28
  • 2
    table_type from the manual: Type of the table: BASE TABLE for a persistent base table (the normal table type), VIEW for a view, FOREIGN for a foreign table, or LOCAL TEMPORARY for a temporary table
    – tzachs
    Mar 11, 2020 at 18:58
50

If you want list of database

SELECT datname FROM pg_database WHERE datistemplate = false;

If you want list of tables from current pg installation of all databases

SELECT table_schema,table_name FROM information_schema.tables
ORDER BY table_schema,table_name;
2
  • At least in Postgres 9.5, that's not true. I've got 3 databases in one cluster, and this is only returning tables from the current database.
    – sudo
    Sep 10, 2016 at 5:52
  • Documentation says only the current one: postgresql.org/docs/9.5/static/infoschema-tables.html "table_catalog sql_identifier Name of the database that contains the table (always the current database)"
    – sudo
    Sep 10, 2016 at 5:53
34

Open up the postgres terminal with the databse you would like:

psql dbname (run this line in a terminal)

then, run this command in the postgres environment

\d

This will describe all tables by name. Basically a list of tables by name ascending.

Then you can try this to describe a table by fields:

\d tablename.

Hope this helps.

3
  • @wingedpanther how to? there are a \d option to list only all tables, with no index, no seq, ...? Dec 3, 2015 at 20:35
  • 5
    Is \dt not there for this?
    – thoroc
    Jan 14, 2016 at 9:15
  • In my experience, wherever I needed to look for table list it was always because I wanted to see if a table exists. Or if do I have a table say for which "contact information" so may I want see all table with "contact" in the name which might come at the beginning, middle, end , anywhere or be just "contact" so I always found schema-vele query to be helpful in that case i.e. SELECT table_name FROM information_schema.tables WHERE table_schema='public' AND table_type='BASE TABLE' and table_name like '%contact%; Rather than going through the list of tables in ascending order.
    – nanosoft
    Mar 24, 2021 at 6:02
17

Try this:

SELECT table_name 
FROM information_schema.tables 
WHERE table_schema='public' AND table_type='BASE TABLE'

this one works!

2
  • I don't think we need table_type='BASE TABLE'. Perhaps I am wrong, could you please elaborate?
    – abriggs
    May 31, 2019 at 15:00
  • sometimes is useful filtering current connected DB adding: and table_catalog = current_database() Nov 28, 2019 at 13:01
11
SELECT table_name
FROM information_schema.tables
WHERE table_type='BASE TABLE'
AND table_schema='public';

For MySQL you would need table_schema='dbName' and for MSSQL remove that condition.

Notice that "only those tables and views are shown that the current user has access to". Also, if you have access to many databases and want to limit the result to a certain database, you can achieve that by adding condition AND table_catalog='yourDatabase' (in PostgreSQL).

If you'd also like to get rid of the header showing row names and footer showing row count, you could either start the psql with command line option -t (short for --tuples-only) or you can toggle the setting in psql's command line by \t (short for \pset tuples_only). This could be useful for example when piping output to another command with \g [ |command ].

10

How about giving just \dt in psql? See https://www.postgresql.org/docs/current/static/app-psql.html.

1
  • Yes, this gives the list of tables within the connected database, not the content of a particular table within the database. This is the answer to the question asked here.
    – Gathide
    Jul 13, 2022 at 7:19
9
select 
 relname as table 
from 
 pg_stat_user_tables 
where schemaname = 'public'

select 
  tablename as table 
from 
  pg_tables  
where schemaname = 'public'
3
  • 1
    pg_stat_user_tables might not be populated if track_activities is disabled. Using the "official" API such as pg_tables or information_schema.table is a much better choice. Oct 13, 2014 at 11:54
  • 1
    side note to this old answer. It looks like information_schema.table only gives table names where you have some access permission. pg_tables allowed me to see names of all tables, even ones I did not have access to. (was useful in an interaction with an administrator)
    – JerryD
    Sep 15, 2021 at 17:01
  • @JerryD Good one!
    – Vivek S.
    Sep 16, 2021 at 10:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.