Difference between set, \ set and \ pset in psql

Several times I am a little embarrassed when working with psql meanwhile, when to use set vs. \set vs. \pset . I think that:

  • set - for session variables when I connect to db. For example SET ROLE dba ;
  • \set 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 never found what, in my opinion, was a good explanation for everyone. Are my assumptions correct?

I am using PostgreSQL 9.4

+5
source share
1 answer

In principle, right. An important difference is that SET is the SQL command, and the other two are psql metadata, indicated by the \ prefix.

  • SET is an SQL command for changing runtime parameters. It runs on the server and has nothing to do with psql per se.

  • \set - psql meta command and for documentation :

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

    Note. This command is not related to the SQL SET command.

  • \pset is another psql meta command. In the documentation :

    This command sets parameters that affect the output of query result tables.

+8
source

All Articles