【问题标题】:psql execute stored function and store the result in the variablepsql 执行存储函数并将结果存储在变量中
【发布时间】:2018-11-21 08:15:19
【问题描述】:

我需要执行sql函数并存储结果

#!/bin/bash  
RESULT=`psql -A -t postgresql://user:password@localhost:5432/db -c "select main.return_anything();" db`
echo $RESULT

并期望结果包含1

但我得到了结果

psql: warning: extra command-line argument "select main.return_anything();" ignored
psql: warning: extra command-line argument "db" ignored

而且它只是在等待某些东西而不产生任何结果。有什么问题?

【问题讨论】:

  • db最后是用户吗?

标签: bash psql


【解决方案1】:

来自psql manual

psql [option...] [dbname [username]]

所以首先是选项,然后是可选的 dbname,然后是可选的用户名。

试试这个:

RESULT=$(psql -A -t -c "select main.return_anything();" postgresql://user:password@localhost:5432/db db)

旁注:反引号 ` 已弃用。使用$(...),它看起来更干净并且可以嵌套。

【讨论】:

  • 最后一个参数db是数据库名?
  • 那么postgresql://user:password@localhost:5432/db在排队做什么?
  • 我的意思是最后一个参数/db [db]<-
  • 我想你只是在postgreqsl://之前错过了-d
  • postgresql://user:password@localhost:5432/db 指定数据库名称db。无需重复两次。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-15
  • 2012-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多