2014-07-10 65 views
0

我試圖列出數據庫中的所有表。 \ dt沒有這樣做,也許是因爲名稱衝突。我試過很多命令,但是當在不同的模式的兩個表共用一個名字,只有一個得到由\ DT上市:如何列出Postgres中的所有表,跨越模式

CREATE DATABASE tester; 
\c tester 
CREATE SCHEMA hid1; 
CREATE SCHEMA hid2; 
CREATE TABLE a (a int); 
CREATE TABLE b (a int); 
CREATE TABLE hid1.a (a int); 
CREATE TABLE hid1.b (a int); 
CREATE TABLE hid1.c (a int); 
CREATE TABLE hid2.a (a int); 
CREATE TABLE hid2.d (a int); 
\dt 
SET search_path TO public,hid1,hid2; 
\dt 
SET search_path TO hid1,public,hid2; 
\dt 
SET search_path TO hid2,hid1,public; 
\dt 

tester=# \dt 
     List of relations 
Schema | Name | Type | Owner 
--------+------+-------+------- 
hid1 | a | table | bob 
hid1 | b | table | bob 
hid1 | c | table | bob 
hid2 | d | table | bob 
(4 rows) 

tester=# SET search_path TO hid2,hid1,public; 
SET 
tester=# \dt 
     List of relations 
Schema | Name | Type | Owner 
--------+------+-------+------- 
hid1 | b | table | bob 
hid1 | c | table | bob 
hid2 | a | table | bob 
hid2 | d | table | bob 
(4 rows) 

看到三個表是如何被屏蔽?我的理解是模式是命名空間。那是我出錯的地方嗎?我錯過了什麼嗎?

回答

5

詢問所有表中的所有模式*

\dt *.* 
+0

啊,很簡單,謝謝。 – enfascination

1

一種選擇是查詢INFORMATION_SCHEMA。這爲您提供了很多使用SQL進行篩選和排序的選項。

select table_catalog, table_schema, table_name 
from information_schema.tables;