296

What is the best way to list all of the tables within PostgreSQL's information_schema?

To clarify: I am working with an empty DB (I have not added any of my own tables), but I want to see every table in the information_schema structure.

9 Answers 9

386

You should be able to just run select * from information_schema.tables to get a listing of every table being managed by Postgres for a particular database.

You can also add a where table_schema = 'information_schema' to see just the tables in the information schema.

7
  • 5
    Thanks, I just tried: /dt (asterisk).(asterisk) is that any different?
    – littleK
    Commented Feb 16, 2010 at 22:10
  • 1
    I don't know anything about the /dt (asterisk).(asterisk) thing, sorry. I just ran the select query in postgres and it listed information about all the tables in it. Try running the select statement (on your blank db) and see what it returns.
    – RodeoClown
    Commented Feb 16, 2010 at 22:15
  • Trying the above command lists the following tables in information_schema: sql_features, sql_implementation_info, sql_languages, sql_packages, sql_parts, sql_sizing, sql_sizing_profiles.....So what's the difference between those tables and the ones in information_schema.tables?
    – littleK
    Commented Feb 16, 2010 at 22:16
  • 2
    All of the tables you have listed (via the /dt command) provide meta-information about the database. Each of the tables listed shows different information. So, for instance, the information_schema.tables table lists all the tables in the database and their attributes (such as being able to see whether it is a table or a view, what the name is and other information like that). The information_schema.sql_features table will show what features are enabled on the database (so I can see that I have Embedded C supported on my database, as well as direct SQL).
    – RodeoClown
    Commented Feb 16, 2010 at 22:20
  • 1
    You can run a select * on each of the tables listed by the dt command - it has just shown you a list of tables containing meta-data on the database.
    – RodeoClown
    Commented Feb 16, 2010 at 22:21
171

For listing your tables use:

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

It will only list tables that you create.

4
  • How about tables that you don't create but you have access permission to?
    – huy
    Commented Mar 6, 2013 at 9:07
  • 7
    This will only show tables in the public schema. You can create tables in other schemas. Commented Jul 12, 2013 at 20:43
  • 2
    Also this won't distinguish between tables and views.
    – jayarjo
    Commented May 7, 2020 at 11:43
  • 1
    @JoeVanDyk I think the fact the you have the where clause literally tells you that. This helps those who would want to list all tables in just one or more schema Commented Jul 11, 2022 at 14:52
50
\dt information_schema.

from within psql, should be fine.

1
  • See comments on OP for more nuances to this option. Commented Sep 19, 2023 at 22:17
22

The "\z" COMMAND is also a good way to list tables when inside the interactive psql session.

eg.

# psql -d mcdb -U admin -p 5555
mcdb=# /z
                           Access privileges for database "mcdb"
 Schema |              Name              |   Type   |           Access privileges
--------+--------------------------------+----------+---------------------------------------
 public | activities                     | table    |
 public | activities_id_seq              | sequence |
 public | activities_users_mapping       | table    |
[..]
 public | v_schedules_2                  | view     | {admin=arwdxt/admin,viewuser=r/admin}
 public | v_systems                      | view     |
 public | vapp_backups                   | table    |
 public | vm_client                      | table    |
 public | vm_datastore                   | table    |
 public | vmentity_hle_map               | table    |
(148 rows)
2
  • 1
    This doesn't list tables in schemas other than public. Commented Aug 2, 2018 at 0:19
  • In the code example, the command is /z when it should be \z.
    – Barry Hess
    Commented Jun 11 at 16:45
19

1.get all tables and views from information_schema.tables, include those of information_schema and pg_catalog.

select * from information_schema.tables

2.get tables and views belong certain schema

select * from information_schema.tables
    where table_schema not in ('information_schema', 'pg_catalog')

3.get tables only(almost \dt)

select * from information_schema.tables
    where table_schema not in ('information_schema', 'pg_catalog') and
    table_type = 'BASE TABLE'
4
  • If you don't filter by table_type, you'll get all sort of objects, such as tables and views mixed up.
    – joninx
    Commented Jul 19, 2019 at 8:08
  • What exactly where table_schema not in ('information_schema', 'pg_catalog') is for?
    – jayarjo
    Commented May 7, 2020 at 11:45
  • 1
    @russellhoff I Did say "get all tables and views" in step 2.
    – hzh
    Commented Jul 18, 2020 at 5:47
  • @jayarjo I used "not in ('information_schema', 'pg_catalog')" to get USER tables and views only.
    – hzh
    Commented Jul 18, 2020 at 5:49
11

You may use also

select * from pg_tables where schemaname = 'information_schema'

In generall pg* tables allow you to see everything in the db, not constrained to your permissions (if you have access to the tables of course).

0
11

For private schema 'xxx' in postgresql :

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

Without table_type = 'BASE TABLE' , you will list tables and views

3

If you want a quick and dirty one-liner query:

select * from information_schema.tables

You can run it directly in the Query tool without having to open psql.

(Other posts suggest nice more specific information_schema queries but as a newby, I am finding this one-liner query helps me get to grips with the table)

0
select * from information_schema.tables where table_schema = 'public' and table_type = 'BASE TABLE'

to get your tables only

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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