Difference between set, \set and \pset in psql

人盡茶涼 提交于 2019-12-06 19:39:03

问题


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


回答1:


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 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



来源:https://stackoverflow.com/questions/29593908/difference-between-set-set-and-pset-in-psql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!