如何在bash中获取psql结果?

发布于 2024-10-28 09:19:51 字数 383 浏览 2 评论 0原文

我试图在 bash 中获取 psql 结果

echo 'SELECT * FROM fictive_table LIMIT 1;' >> /tmp/x.sql
psql --single-transaction -d dbname -f /tmp/x.sql
echo $?

这会失败,但我在 bash 中得到的结果是 0。逻辑/代码有问题吗?

来自手册

如果 psql 正常完成,则向 shell 返回 0;如果发生致命错误,则返回 1 发生自身错误(内存不足、找不到文件)

更新: @Andrea Spadaccini 是对的。这并不致命。我怎样才能抓住它?

I'm trying to get the psql result in bash

echo 'SELECT * FROM fictive_table LIMIT 1;' >> /tmp/x.sql
psql --single-transaction -d dbname -f /tmp/x.sql
echo $?

This will fail but the result I get in bash is 0. Is there a problem with the logic/code?

From the manual

psql returns 0 to the shell if it finished normally, 1 if a fatal
error of its own (out of memory, file not found) occurs

Update:
@Andrea Spadaccini is right. This is not fatal. How can I catch it ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

三岁铭 2024-11-04 09:19:51

也许查询触发的错误并不像手册中提到的那样致命,即您不会耗尽内存,也不会出现文件未找到错误。

进一步阅读手册,我读到:

如果 psql 正常完成,则向 shell 返回 0;如果发生自身的致命错误(内存不足、未找到文件),则返回 1;如果与服务器的连接出现故障且会话无法交互,则返回 2; 3 如果脚本中发生错误并且设置了变量 ON_ERROR_STOP。

要捕获错误,您可以尝试:

  • 设置 ON_ERROR_STOP 变量;或者,
  • 更改方法,将查询输出保存到文件中,然后使用该文件。

Maybe the kind of error triggered by the query is not a fatal one like the ones mentioned in the manual, i.e., you don't run out of memory and it does not get a file not found error.

Reading a bit further in the manual, I read that:

psql returns 0 to the shell if it finished normally, 1 if a fatal error of its own (out of memory, file not found) occurs, 2 if the connection to the server went bad and the session was not interactive, and 3 if an error occurred in a script and the variable ON_ERROR_STOP was set.

To catch your error you can try to:

  • set the ON_ERROR_STOP variable; or,
  • change approach, saving the query output to a file and then working with that file.
枕花眠 2024-11-04 09:19:51

使用选项 -o 将查询结果从 psql(命令行终端)写入文件。
每个文档

-o filename
--output=filename

Put all query output into file filename. This is equivalent to the command \o.

Use the option -o to write results of a query from psql (the command line terminal) to a file.
Per documentation:

-o filename
--output=filename

Put all query output into file filename. This is equivalent to the command \o.
苄①跕圉湢 2024-11-04 09:19:51

[我知道...这是...]

我发现这适用于我从 bash 运行 psql 命令的用例:

$ echo "command-to-run" |(sudo -u postgres psql -t -d <dbname> -f -)

我在我的 nextcloud 服务器上使用 psql - 这是一个示例命令:

$ echo "select uid from oc_users where uid like '%thd%';" |(sudo -u postgres psql -t -d nextcloud -f -)
 [email protected]
 [email protected]
 [email protected]
 [email protected]

[I know... this is old...]

I discovered that this works for my use case for running psql commands from bash:

$ echo "command-to-run" |(sudo -u postgres psql -t -d <dbname> -f -)

I'm using psql on my nextcloud server - here's a sample command:

$ echo "select uid from oc_users where uid like '%thd%';" |(sudo -u postgres psql -t -d nextcloud -f -)
 [email protected]
 [email protected]
 [email protected]
 [email protected]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文