22

I get a little confused some times when working with psql between when to use a set vs. \set vs. \pset. I think that:

  • set is for session variables on my connection to the db. For example SET 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'

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

4

1 回答 1

22

基本正确。重要的区别在于它SET是一个 SQL 命令,而另外两个是 psql 元命令 - 由前缀表示\

  • SET是用于更改运行时参数的 SQL 命令。它在服务器上执行,与 psql 本身无关。

  • \set是一个 psql 元命令,并且根据文档

将 psql 变量名称设置为值 [...]

注意:此命令与 SQL 命令无关SET

此命令设置影响查询结果表输出的选项

于 2015-04-12T20:38:48.703 回答