PyGreSQL 运行 SQL 交互

PyGreSQL Run SQL interactively

我有一个 GreenPlum 数据库,我正在尝试创建几个外部表。

我正在使用 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' " 和其他组合。

我不确定通用客户端是否可以使用这些别名。

您可以通过首先使用 Python 读取文件 h1/ext/eng/ext_eng.sql,然后使用 [=] 将生成的 SQL 文本流式传输到 PyGreSQL 来避免 \i 15=].

至于 \dt、\du 和 \dv - SQL 等价物可以由 运行 \set ECHO_HIDDEN 从 psql 提示符中看到,然后发出每一个以查看真实的定义。我在下面包含了结果。

\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;

\du:

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 引用替换为这些等效代码。