【问题标题】:Running system command in Postgres and use return value in Insert在 Postgres 中运行系统命令并在 Insert 中使用返回值
【发布时间】:2020-04-07 02:07:04
【问题描述】:

我有一个 linux 命令,我想在以下函数中运行:

CREATE FUNCTION tarifador_func2() RETURNS TRIGGER
LANGUAGE plsh
AS $$
#!/bin/sh
SET userid[] = // here i want to run linux command and use that return value to insert into below table
for loop with i index
INSERT INTO public.act_id_priv_mapping(id_, priv_id_, user_id_, group_id_) VALUES (auuid,new.priv_id_,userid[i],new.group_id_);
$$;

我有以下值通过新行命令返回:

ankit
ankit1

谁能帮我完成我的功能。谢谢

【问题讨论】:

  • 这看起来根本不像 shell 代码。我对它不适用于 PL/sh 并不感到惊讶。那应该是什么语言?
  • @LaurenzAlbe 请检查我编辑的问题,我在其中添加了#!/bin/sh
  • 但是您所写的根本不是 shell 代码,并且该注释不会改变这一点。为什么不使用 PL/Python 或 PL/Perl 并从那里运行系统命令?
  • @LaurenzAlbe 我是脚本新手。如果你能提供我在 PL/Python 中的概念,那么请提供给我。
  • 那么你的 PostgreSQL 是在没有 Python 支持的情况下构建的。相信我,你不想在 C 中解决这个问题。

标签: postgresql plpython


【解决方案1】:

你应该以不同的方式解决这个问题。

使用COPY ... FROM PROGRAM 执行shell 命令并使用结果填充(临时?)表。

【讨论】:

  • 感谢您的回答,但我在创建外部表时收到错误 SQL Error [HV00D]: ERROR: invalid option "program" Hint: Valid options in this context are: filename, format, header, delimiter, quote, escape, null, encoding
  • 对。您不需要PROGRAM 选项。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-05
  • 2012-05-25
  • 2013-04-01
  • 1970-01-01
  • 2019-03-23
  • 2011-03-14
  • 2011-11-03
相关资源
最近更新 更多