ps列出所有表

pet*_*hel 121 postgresql psql

我想liferay在PostgreSQL安装中列出数据库中的所有表.我怎么做?

我想SELECT * FROM applications;liferay数据库中执行.applications是我liferay db中的一个表.这是怎么做到的?

这是我所有数据库的列表:

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

Cra*_*ger 191

如果要列出所有表,则必须使用:

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

表示您想要所有模式中的所有表.这将包括表pg_catalog,系统表和中的表information_schema.没有内置的方式来说"所有用户定义的模式中的所有表"; 但是,您可以search_path在运行之前将所有模式设置为列表\dt.

您可能希望以编程方式执行此操作,在这种情况下,psql反斜杠命令将无法完成此任务.这是INFORMATION_SCHEMA来救援.要列出表格:

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

顺便说一句,如果您想查看psql响应反斜杠命令的操作,请psql使用-E标志运行.例如:

$ 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;
**************************
Run Code Online (Sandbox Code Playgroud)

所以你可以看到它在获取数据库列表时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;
Run Code Online (Sandbox Code Playgroud)

在可能的情况下,最好使用SQL标准,可移植INFORMATION_SCHEMA而不是Pg系统目录,但有时您需要特定于Pg的信息.在这些情况下,可以直接查询系统目录,并且psql -E可以作为如何执行此操作的有用指南.


d11*_*wtq 91

连接到数据库,然后列出表:

\c liferay
\dt
Run Code Online (Sandbox Code Playgroud)

无论如何,我就是这样做的.

如果您愿意,可以将这两个命令组合到一行中:

\c liferay \dt
Run Code Online (Sandbox Code Playgroud)

  • 你真的想要`\ dt*.*`如果不是所有感兴趣的表都在`search_path`上. (2认同)
  • 感谢`\ c db_name` (2认同)

nik*_*ypx 8

要查看公共表,您可以执行此操作

列表

\dt
Run Code Online (Sandbox Code Playgroud)

列表,视图和访问权限

\dp or \z
Run Code Online (Sandbox Code Playgroud)

或只是表名

select table_name from information_schema.tables where table_schema = 'public';
Run Code Online (Sandbox Code Playgroud)


小智 6

在SQL查询中,您可以编写以下代码:

select table_name from information_schema.tables where table_schema='YOUR_TABLE_SCHEME';
Run Code Online (Sandbox Code Playgroud)

将您的表方案替换为 YOUR_TABLE_SCHEME;

例子:

select table_name from information_schema.tables where table_schema='eLearningProject';
Run Code Online (Sandbox Code Playgroud)

要查看所有方案和所有表,不需要 where 子句:

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