2016-07-15 151 views
1

我有一個GreenPlum數據庫,並且我試圖創建幾個外部表。PyGreSQL交互式運行SQL

我正在使用PyGreSQL包與數據庫進行接口。

但是,我不能逃避\i(或\dt,\du,\dv)。

db.query("\i h1/ext/eng/ext_eng" + i + ".sql")

pg.ProgrammingError: ERROR: syntax error at or near "\"

我試過"\\i"" '\i' "等組合。

+0

我想出瞭如何使用[this](http://superuser.com/questions/701415/how-do-i-execute-run-a-sql-file-in-postgresql-9-2 -6- centos的) – syuja

回答

1

我不確定那些別名可以被通用客戶端使用。

您可以通過先用Python讀取h1/ext/eng/ext_eng.sql文件,然後將產生的SQL文本以db.query()流式傳輸到PyGreSQL來避免\i

至於\ dt,\ du和\ dv - 可以通過在psql提示符下運行\set ECHO_HIDDEN來查看SQL等價物,然後發出每個以查看真正的定義。我已經包含了下面的結果。

\ DT:

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' END as "Type", 
    pg_catalog.pg_get_userbyid(c.relowner) as "Owner", CASE c.relstorage WHEN 'h' THEN 'heap' WHEN 'x' THEN 'external' WHEN 'a' THEN 'append only' WHEN 'v' THEN 'none' WHEN 'c' THEN 'append only columnar' WHEN 'p' THEN 'parquet' WHEN 'f' THEN 'foreign' END as "Storage" 

FROM pg_catalog.pg_class c 
    LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace 
WHERE c.relkind IN ('r','') 
AND c.relstorage IN ('h', 'a', 'c', 'p','') 
     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; 

\杜:

SELECT r.rolname, r.rolsuper, r.rolinherit, 
    r.rolcreaterole, r.rolcreatedb, r.rolcanlogin, 
    r.rolconnlimit, 
    ARRAY(SELECT b.rolname 
     FROM pg_catalog.pg_auth_members m 
     JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid) 
     WHERE m.member = r.oid) as memberof 
FROM pg_catalog.pg_roles r 
ORDER BY 1; 

\ DV:

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' END as "Type", 
    pg_catalog.pg_get_userbyid(c.relowner) as "Owner", CASE c.relstorage WHEN 'h' THEN 'heap' WHEN 'x' THEN 'external' WHEN 'a' THEN 'append only' WHEN 'v' THEN 'none' WHEN 'c' THEN 'append only columnar' WHEN 'p' THEN 'parquet' WHEN 'f' THEN 'foreign' END as "Storage" 

FROM pg_catalog.pg_class c 
    LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace 
WHERE c.relkind IN ('v','') 
AND c.relstorage IN ('v','') 
     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; 

這不是理想的,但你可以用這些代碼等價物的\ d引用。