psql中set,\set和\pset之间的区别 [英] Difference between set, \set and \pset in psql

查看:1060
本文介绍了psql中set,\set和\pset之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 psql 和何时使用设置和<$时,我有时会感到困惑c $ c> \set 与 \pset 的对比。我认为:

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 是用于连接数据库的会话变量。例如 SET ROLE dba ;

  • \set 用于局部变量对于此psql会话。例如 \设置时间'select current_timestamp'

  • \pset 用于此psql会话的psql设置。例如'\pset border 2'

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

我正在使用PostgreSQL 9.4

I'm using PostgreSQL 9.4

推荐答案

基本正确。重要的区别是 SET 是SQL命令,而其他两个是psql元命令-由前缀 \

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 是一个psql元命令,每个文档

\set is a psql meta-command and, per documentation:


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

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

该命令与SQL命令 SET 无关。

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


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

  • \pset is another psql meta-command. Per documentation:


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

    This command sets options affecting the output of query result tables


  • 这篇关于psql中set,\set和\pset之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆