2012-09-16 58 views
101

我想在我的PostgreSQL安裝中列出liferay數據庫中的所有表格。我怎麼做?Psql列出所有表格

我想在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=# 

回答

163

,你必須使用:

\dt *.* 

表明,你需要在所有模式中的所有表。這將包括pg_catalog中的表格,系統表格以及information_schema中的表格。沒有內置的方式來表示「所有用戶定義的模式中的所有表」;但是,您可以在運行\dt之前將您的search_path設置爲所有感興趣的模式列表。

您可能希望以編程方式執行此操作,在這種情況下,psql反斜槓命令將不會執行此作業。這是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可以作爲一個有用的指導。

+0

哇!非常感謝你的順便說一句。這非常有價值。 –

+0

'information_schema.tables'包含某些原因的視圖。 (無論如何,在PostgreSQL 9.2中。) – jpmc26

+0

@ jpmc26是的,'table_type ='VIEW'',所以它們很容易排除。一般來說,SQL儘可能地嘗試將表視爲與表相同。 –

85

連接到數據庫,然後列出表:

\c liferay 
\dt 

這就是我怎麼做也無妨。

可以這兩個命令組合成一條線,如果你喜歡:如果您希望列出所有

\c liferay \dt 
+2

你真的想'\ DT * *'如果不是所有感興趣的表是在'SEARCH_PATH。 '。 –

+2

感謝'\ c db_name' –

+0

我得到的只是'沒有找到關係.' – surfer190

4

要看到公共表,你可以做

清單表

\dt 

名單表,視圖和訪問權限

\dp or \z 

或只是表名

select table_name from information_schema.tables where table_schema = 'public'; 
1

在SQL Que RY,你可以這樣寫代碼:

select table_name from information_schema.tables where table_schema='YOUR_TABLE_SCHEME'; 

替換YOUR_TABLE_SCHEME您的表計劃;

例子:

select table_name from information_schema.tables where table_schema='eLearningProject'; 

要看到所有方案和所有的表,也沒有必要的where子句:

select table_name from information_schema.tables 
-1

您可以鍵入\?獲得在PSQL支持的所有命令的信息。

0

這可以在自動化腳本中使用,如果你並不需要所有表中的所有模式:

for table in $(psql -qAntc '\dt' | cut -d\| -f2); do 
     ... 
    done