Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between set, \set and \pset in psql

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

like image 518
David S Avatar asked Apr 12 '15 19:04

David S


People also ask

What is Gset in PostgreSQL?

\gset is a new meta-command since psql 9.3. If you have an older version, it's much harder to emulate this. Description from the manpage: \gset [ prefix ] Sends the current query input buffer to the server and stores the query's output into psql variables (see Variables).

When using PostgreSQL which two different approaches can you take to access the database?

You may access the database from a programming language, through a GUI application that allows access and administration, or through the command-line interface.

What is real datatype in PostgreSQL?

real or float8 is a 4-byte floating-point number. numeric or numeric(p,s) is a real number with p digits with s number after the decimal point. The numeric(p,s) is the exact number.


1 Answers

Basically correct. The important difference is that SET is an SQL command while the other two are psql meta-commands - indicated by the leading \.

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:

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

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

\pset is another psql meta-command:

This command sets options affecting the output of query result tables

like image 52
Erwin Brandstetter Avatar answered Oct 16 '22 07:10

Erwin Brandstetter