PostgreSQL 查询列出所有表名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14730228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 00:01:27  来源:igfitidea点击:

PostgreSQL query to list all table names?

postgresqlpostgresql-9.2

提问by jobi88

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

是否有任何查询可用于列出我的 Postgres 数据库中的所有表。

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?

我怎样才能只得到表名,而不是视图?

回答by vyegorov

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';

回答by Harsh

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

如果您想要所有数据库的当前 pg 安装中的表列表

SELECT table_schema,table_name FROM information_schema.tables
ORDER BY table_schema,table_name;

回答by Ty_

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

使用您想要的数据库打开 postgres 终端:

psql dbname (run this line in a terminal)

then, run this command in the postgres environment

然后,在 postgres 环境中运行此命令

\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.

希望这可以帮助。

回答by T.S.

Try this:

尝试这个:

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

this one works!

这个有效!

回答by Vivek S.

select 
 relname as table 
from 
 pg_stat_user_tables 
where schemaname = 'public'


select 
  tablename as table 
from 
  pg_tables  
where schemaname = 'public'

回答by Baris Demiray

回答by Kashif

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.

对于 MySQL,您需要 table_schema='dbName' 并且对于 MSSQL 删除该条件。

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).

请注意,“仅显示当前用户有权访问的那些表和视图”。此外,如果您可以访问许多数据库并希望将结果限制在某个数据库中,您可以通过添加条件 AND table_catalog='yourDatabase'(在 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 ].

如果您还想去掉显示行名称的标题和显示行数的页脚,您可以使用命令行选项 -t(--tuples-only 的缩写)启动 psql,或者您可以切换 psql 中的设置\t 的命令行(\pset tuples_only 的缩写)。例如,当使用 \g [ |command ] 将输出传送到另一个命令时,这可能很有用。