将postgresql结果存储在bash变量中 [英] store postgresql result in bash variable

查看:98
本文介绍了将postgresql结果存储在bash变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何像下面的脚本那样在bash变量上赋予标量postgresql值?

How to atore a scalar postgresql-value on a bash-variable like in script below?

dbname="testlauf"
username="postgres"

vartest='psql -c -d $dbname -U $username -h localhost -p 5432 "SELECT gid FROM testtable WHERE aid='1';"'
echo "$vartest"

我尝试了几种不同的著作,但似乎没有任何效果。

I tried several different writings, but nothing seems to work. Thanks in advance.

推荐答案

-c 选项放在其前面参数-查询。注意,还可以使用附加的 -t 选项仅获取元组值。当然,请使用反引号(`)运算符。

Put the -c option just before its argument - the query. Mind also using the additional -t option to get just the tuple value. And of course, use the backticks (`) operator.

使用 -X 选项,因为有时 .psqlrc 文件可能会添加一些冗余输出,以及 -A 选项,该选项将禁用列对齐(空格)。

Using the -X option is also recommended, as sometimes a .psqlrc file might add some redundant output, as well as the -A option, which disables column aligning (whitespaces).

vartest=`psql -X -A -d $dbname -U $username -h localhost -p 5432 -t -c "SELECT gid FROM testtable WHERE aid='1'"`

这篇关于将postgresql结果存储在bash变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆