postgresql psql 中 set、\set 和 \pset 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29593908/
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
Difference between set, \set and \pset in psql
提问by David S
I get a little confused some times when working with psql
between when to use a set
vs. \set
vs. \pset
. I think that:
在处理psql
何时使用 a set
vs. \set
vs.之间时,我有时会感到有些困惑\pset
。我觉得:
set
is for session variables on my connection to the db. For exampleSET ROLE dba
;\set
is for local variables for this psql session. For example\set time 'select current_timestamp'
\pset
is for psql settings for this psql session. For example'\pset border 2'
set
用于我与数据库的连接上的会话变量。例如SET ROLE dba
;\set
用于此 psql 会话的局部变量。例如\set time 'select current_timestamp'
\pset
用于此 psql 会话的 psql 设置。例如'\pset border 2'
But, I've never found what I thought was a good explanation of each. Are my assumptions above correct?
但是,我从来没有找到我认为对每一个都很好的解释。我上面的假设是否正确?
I'm using PostgreSQL 9.4
我正在使用 PostgreSQL 9.4
采纳答案by Erwin Brandstetter
Basically correct. The important difference is that SET
is an SQL command while the other two are psql meta-commands - indicated by the prefixed \
.
基本正确。重要的区别在于它SET
是一个 SQL 命令,而另外两个是 psql 元命令 - 由前缀\
.
SET
is an SQL command to change run-time parameters. It is executed on the server and has nothing to do with psql per se.\set
is a psql meta-command and, per documentation:Sets the psql variable name to value [...]
Note: This command is unrelated to the SQL command
SET
.\pset
is another psql meta-command. Per documentation:This command sets options affecting the output of query result tables