PostgreSQL query to list all table names?

时光毁灭记忆、已成空白 提交于 2019-11-28 15:31:52

问题


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?


回答1:


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:


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;



回答3:


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.




回答4:


select 
 relname as table 
from 
 pg_stat_user_tables 
where schemaname = 'public'
  • This will not work if track_activities is disabled

select 
  tablename as table 
from 
  pg_tables  
where schemaname = 'public'
  • Read more about pg_tables



回答5:


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




回答6:


Try this:

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

this one works!



来源:https://stackoverflow.com/questions/14730228/postgresql-query-to-list-all-table-names

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!