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

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

Difference between set, \set and \pset in psql

postgresqlpsqlpostgresql-9.4

提问by David S

I get a little confused some times when working with psqlbetween when to use a setvs. \setvs. \pset. I think that:

在处理psql何时使用 a setvs. \setvs.之间时,我有时会感到有些困惑\pset。我觉得:

  • setis for session variables on my connection to the db. For example SET ROLE dba;
  • \setis for local variables for this psql session. For example \set time 'select current_timestamp'
  • \psetis 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 SETis an SQL command while the other two are psql meta-commands - indicated by the prefixed \.

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

  • SETis an SQL command to change run-time parameters. It is executed on the server and has nothing to do with psql per se.

  • \setis a psql meta-command and, per documentation:

    Sets the psql variable name to value [...]

    Note: This command is unrelated to the SQL command SET.

  • \psetis another psql meta-command. Per documentation:

    This command sets options affecting the output of query result tables

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

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

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

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

  • \pset是另一个 psql 元命令。根据文档

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