2016-11-28 62 views
0

使用的是Postgres我已經寫了一個查詢列出數據庫中的表從數據庫錯誤如何查詢從一個數據庫到另一個

SELECT table_name 
FROM information_schema.tables 
WHERE table_type = 'BASE TABLE' 
AND table_schema NOT IN 
('pg_catalog', 'information_schema'); 

查詢返回的結果。它會自動從postgres中的數據庫列表中選擇第一個數據庫。

如何指定要查詢的數據庫? 「j220190_data」是對數據庫進行查詢

我已經試過了諸如:當您連接到Postgres的選擇數據庫

SELECT table_name 
FROM information_schema.tables 
WHERE Databases = 'j220190_data' 
AND table_schema NOT IN 
('pg_catalog', 'information_schema'); 

SELECT table_name 
FROM information_schema.tables 
WHERE table_type = 'BASE TABLE' AND 
WHERE Databases = 'j220190_data' 
AND table_schema NOT IN 
('pg_catalog', 'information_schema'); 
+2

... – jarlh

+0

「*我如何指定數據庫查詢*」,「工作正常,但它不工作。」 - 你不能,你需要先_connect_到其他數據庫。 Postgres不支持跨數據庫查詢 –

+0

「*它自動從postgres *中的數據庫列表中選擇第一個數據庫」 - 這是錯誤的。該查詢返回當前連接到的數據庫的信息。 –

回答

0

;
例如:

psql mydb 

並在發佈的選擇後,因爲當你嘗試白衣這個選擇,對Postgres的默認數據庫檢查。

+0

錯誤:「psql」處或附近的語法錯誤? – John

+1

@John:[psql](https://www.postgresql.org/docs/current/static/reference-client.html)是**命令行**程序,而不是SQL語句。 –

+0

當你在postgres數據庫連接時,你可以在數據庫上連接你想要的,如果你的數據庫是這樣的話,你可以用psql j220190_data嘗試。你明顯地從shell連接 –

0

如果我理解正確,您可以使用dblink()查詢另一個數據庫。

創建擴展CREATE EXTENSION dblink;

SELECT tbl.* 
FROM dblink('dbname=DB1 port=5432 
      host=localhost user=usr password=123', 'SELECT table_name 
FROM information_schema.tables 
WHERE table_type = ''BASE TABLE'' 
AND table_schema NOT IN(''pg_catalog'', ''information_schema'');') 
AS tbl(table_name varchar(30)); 
相關問題