【发布时间】:2011-01-17 14:41:32
【问题描述】:
在 PostgreSQL 的 information_schema 中列出所有表的最佳方式是什么?
澄清一下:我正在使用一个空数据库(我没有添加任何我自己的表),但我想查看 information_schema 结构中的每个表。
【问题讨论】:
标签: sql postgresql information-schema
在 PostgreSQL 的 information_schema 中列出所有表的最佳方式是什么?
澄清一下:我正在使用一个空数据库(我没有添加任何我自己的表),但我想查看 information_schema 结构中的每个表。
【问题讨论】:
标签: sql postgresql information-schema
您应该能够只运行select * from information_schema.tables 以获取 Postgres 为特定数据库管理的每个表的列表。
您还可以添加where table_schema = 'information_schema' 以仅查看信息架构中的表。
【讨论】:
要列出您的表格,请使用:
SELECT table_name FROM information_schema.tables WHERE table_schema='public'
它只会列出您创建的表。
【讨论】:
\dt information_schema.
在 psql 中,应该没问题。
【讨论】:
"\z" 命令 也是在交互式 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)
【讨论】:
1.从information_schema.tables中获取所有表和视图,包括information_schema和pg_catalog的。
select * from information_schema.tables
2.获取表和视图属于某个模式
select * from information_schema.tables
where table_schema not in ('information_schema', 'pg_catalog')
3.仅获取表(几乎\dt)
select * from information_schema.tables
where table_schema not in ('information_schema', 'pg_catalog') and
table_type = 'BASE TABLE'
【讨论】:
where table_schema not in ('information_schema', 'pg_catalog') 到底是干什么用的?
你也可以用
select * from pg_tables where schemaname = 'information_schema'
一般而言,pg* 表允许您查看数据库中的所有内容,不受您的权限限制(当然,如果您有权访问这些表)。
【讨论】:
对于 postgresql 中的私有模式 'xxx':
SELECT table_name FROM information_schema.tables
WHERE table_schema = 'xxx' AND table_type = 'BASE TABLE'
没有 table_type = 'BASE TABLE' ,您将列出表格和视图
【讨论】:
如果你想要一个快速而肮脏的单行查询:
select * from information_schema.tables
可以直接在查询工具中运行,无需打开psql。
(其他帖子建议更好的更具体的 information_schema 查询,但作为一个新手,我发现这个单行查询可以帮助我掌握表格)
【讨论】: