列出postgresql information_schema中的所有表

lit*_*leK 171 sql postgresql information-schema

在PostgreSQL的information_schema中列出所有表的最佳方法是什么?

澄清:我正在使用一个空数据库(我没有添加任何自己的表),但我希望看到information_schema结构中的每个表.

Rod*_*own 240

您应该能够运行select * from information_schema.tables以获取Postgres为特定数据库管理的每个表的列表.

您还可以添加a where table_schema = 'information_schema'以仅查看信息架构中的表.

  • 谢谢,我刚试过:/ dt(星号).(星号)是不是有什么不同? (4认同)
  • 您列出的所有表(通过/ dt命令)都提供有关数据库的元信息.列出的每个表都显示不同的信息.因此,例如,information_schema.tables表列出了数据库中的所有表及其属性(例如能够查看它是表还是视图,名称是什么以及其他类似的信息).information_schema.sql_features表将显示数据库上启用了哪些功能(因此我可以看到我的数据库支持嵌入式C,以及直接SQL). (2认同)

小智 97

要列出您的表,请使用:

SELECT table_name FROM information_schema.tables WHERE table_schema='public'
Run Code Online (Sandbox Code Playgroud)

它只会列出您创建的表.

  • 这只会显示公共模式中的表.您可以在其他模式中创建表. (3认同)

小智 40

\dt information_schema.
Run Code Online (Sandbox Code Playgroud)

从psql内部,应该没问题.


小智 14

"\ Z" COMMAND也是互动PSQL会话内部时列出的表的好方法.

例如.

# 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)
Run Code Online (Sandbox Code Playgroud)


Tim*_*fey 9

你也可以用

select * from pg_tables where schemaname = 'information_schema'
Run Code Online (Sandbox Code Playgroud)

通常,pg*表允许您查看数据库中的所有内容,而不是限制您的权限(如果您当然可以访问表).


ger*_*nux 7

对于'xxx'postgresql中的私有模式:

SELECT table_name FROM information_schema.tables 
 WHERE table_schema = 'xxx' AND table_type = 'BASE TABLE'
Run Code Online (Sandbox Code Playgroud)

如果没有table_type = 'BASE TABLE',您将列出表格和视图


小智 6

1.从information_schema.tables中获取所有表和视图,包括information_schema和pg_catalog。

select * from information_schema.tables
Run Code Online (Sandbox Code Playgroud)

2.获取表和视图属于特定架构

select * from information_schema.tables
    where table_schema not in ('information_schema', 'pg_catalog')
Run Code Online (Sandbox Code Playgroud)

3.仅获取表(几乎\ dt)

select * from information_schema.tables
    where table_schema not in ('information_schema', 'pg_catalog') and
    table_type = 'BASE TABLE'
Run Code Online (Sandbox Code Playgroud)