将 PostgreSQL 查询结果存储到 Shell 或 PostgreSQL 变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12535766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 00:19:49  来源:igfitidea点击:

Store PostgreSQL query result to Shell or PostgreSQL variable

postgresqlshell

提问by Rock

For instance, I have a table stores value:

例如,我有一个表存储值:

select * from myvalue;

  val
-------
 12345
(1 row)

How can I save this 12345into a variable in postgresql or shell script?

如何将其保存12345到 postgresql 或 shell 脚本中的变量中?

Here's what I tried in my shell script:

这是我在 shell 脚本中尝试的内容:

var=$(psql -h host -U user -d db <<SQLSTMT
SELECT * FROM myvalue;
SQLSTMT)

but echo $vargives me:

echo $var给了我:

val ------- 12345 (1 row)

I've also tried

我也试过

\set var (select * from myvalue)

in psql and when I type \setit lists:

在 psql 中,当我键入\set它时列出:

var = '(select*frommyvalue)'

回答by Piotr Wadas

No, no, no! Use "raw data" switch from psql, like "-t" or "\t" and pipe the query to psql instead of parsing ascii-table, come on :-)

不不不!使用 psql 中的“原始数据”开关,例如“-t”或“\t”并将查询通过管道传输到 psql 而不是解析 ascii-table,来吧:-)

echo 'select * from myvalue;' | psql -t -h host -U user -d db

If you really need parse psql output, you could also use -H switch ( turns on HTML output ), and parse it with some perl module for parsing html tables, I used that once or twice.. Also, you may want to use a pgpass fileand ~/.psqlrcfor some defaults, like default DB to connect, when not specified.

如果你真的需要解析 psql 输出,你也可以使用 -H 开关(打开 HTML 输出),并用一些 perl 模块解析它来解析 html 表,我用过一两次。另外,你可能想使用pgpass 文件~/.psqlrc一些默认值,如默认数据库连接,当未指定时。

回答by willglynn

psqlhas a -c/--command=option to accept SQL from the command line, and -t/--tuples-onlyoption to control output formatting.

psql有一个-c/--command=选项可以从命令行接受 SQL,还有-t/--tuples-only选项可以控制输出格式。

$ psql -c 'select 1+1'
 ?column? 
----------
        2
(1 row)
$ psql -t -c 'select 1+1'
        2

$ VALUE=`psql -t -c 'select 1+1'`
$ echo $VALUE
2

回答by Scott Marlowe

var=`psql -Atc "select 1;"`
echo $var
1

回答by Stephane Rouberol

You can filter the result you get with your psqlcommand:

您可以使用psql命令过滤得到的结果:

var=$(psql -h host -U user -d db <<SQLSTMT
SELECT * FROM myvalue;
SQLSTMT)
var=$(cut -d' ' -f3 <<<$var)

回答by Craig Ringer

In this answerI explain one way to do it, using a co-process to communicate back-and-forth with psql. That's overkill if all you need is to run a query and get a single result, but might be good to know if you're shell scripting with psql.

这个答案中,我解释了一种方法,即使用协同进程与psql. 如果您所需要的只是运行查询并获得单个结果,那就有点矫枉过正了,但如果您使用psql.

回答by Cassandra Rodriguez

None of these worked for me, but this did:

这些都不适合我,但这样做了:

median_avm=psql "host=${dps1000} port=#### dbname=@@@ user=${reduser} password=${redpass}" -c "SELECT AVG(column) FROM db.table;" -t

中值_avm=psql "host=${dps1000} port=#### dbname=@@@ user=${reduser} password=${redpass}" -c "SELECT AVG(column) FROM db.table;" -t

using a source file with ${dps1000}, ${reduser}, ${redpass} defined and manually entering port and dbname

使用定义了 ${dps1000}、${reduser}、${redpass} 的源文件并手动输入端口和数据库名称