我想在我的 PostgreSQL 安装中列出 liferay
数据库中的所有表。我该怎么做?
我想在 liferay
数据库中执行 SELECT * FROM applications;
。 applications
是我的 liferay 数据库中的一个表。这是怎么做到的?
这是我所有数据库的列表:
postgres=# \list
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
liferay | postgres | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 | =Tc/postgres +
| | | | | postgres=CTc/postgres+
| | | | | liferay=CTc/postgres
lportal | postgres | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 |
postgres | postgres | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 |
template0 | postgres | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(5 rows)
postgres=#
最佳答案
如果你想列出所有表,你必须使用:
\dt *.*
表明您想要所有模式中的所有表。这将包括 pg_catalog
中的表、系统表和 information_schema
中的表。没有内置的方式来表示“所有用户定义模式中的所有表”;但是,您可以在运行 \dt
之前将您的 search_path
设置为所有感兴趣模式的列表。
您可能希望以编程方式执行此操作,在这种情况下,psql
backslash-commands 将无法完成这项工作。这是the INFORMATION_SCHEMA
的地方来救援。列出表格:
SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';
顺便说一句,如果你想看看 psql
在响应反斜杠命令时做了什么,请使用 -E
标志运行 psql
.例如:
$ psql -E regress
regress=# \list
********* QUERY **********
SELECT d.datname as "Name",
pg_catalog.pg_get_userbyid(d.datdba) as "Owner",
pg_catalog.pg_encoding_to_char(d.encoding) as "Encoding",
d.datcollate as "Collate",
d.datctype as "Ctype",
pg_catalog.array_to_string(d.datacl, E'\n') AS "Access privileges"
FROM pg_catalog.pg_database d
ORDER BY 1;
**************************
因此您可以看到 psql
在获取数据库列表时正在搜索 pg_catalog.pg_database
。同样,对于给定数据库中的表:
SELECT n.nspname as "Schema",
c.relname as "Name",
CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' END as "Type",
pg_catalog.pg_get_userbyid(c.relowner) as "Owner"
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r','')
AND n.nspname <> 'pg_catalog'
AND n.nspname <> 'information_schema'
AND n.nspname !~ '^pg_toast'
AND pg_catalog.pg_table_is_visible(c.oid)
ORDER BY 1,2;
如果可能,最好使用 SQL 标准、可移植的 INFORMATION_SCHEMA
而不是 Pg 系统目录,但有时您需要特定于 Pg 的信息。在这些情况下,可以查询 system catalogs直接,psql -E
可以作为一个有用的指南来指导如何这样做。
关于postgresql - psql 列出所有表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12445608/