For example, you can set the custom options my.num
and my.first.name
as variables as shown below. *A custom option can have multiple .
and custom options are deleted after logout:
SET my.num = 2;
SET my.first.name = 'John';
Or, you can set the custom options with set_config() as shown below. *For set_config()
, false
in the 3rd argument means the new value applies to the current session and true
in the 3rd argument means the new value applies to the current transaction and my answer explains set_config()
in detail:
SELECT set_config('my.num', '2', false);
SELECT set_config('my.first.name', 'John', false);
Then, you must use current_setting() to get the values of the custom options my.num
and my.first.name
as shown below:
postgres=# SELECT current_setting('my.num');
current_setting
-----------------
2
(1 row)
postgres=# SELECT current_setting('my.first.name');
current_setting
-----------------
John
(1 row)
Be careful, setting a custom option without .
gets the error as shown below:
postgres=# SET num = 2;
ERROR: unrecognized configuration parameter "num"
Or:
postgres=# SELECT set_config('num', '2', false);
ERROR: unrecognized configuration parameter "num"
And, 2
without ''
in set_config()
gets the error as shown below:
postgres=# SELECT set_config('my.num', 2, false);
ERROR: function set_config(unknown, integer, boolean) does not exist
LINE 1: SELECT set_config('my.num', 2, false);
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
And, using a custom option without current_setting()
cannot get the value as shown below:
postgres=# SELECT 'my.num';
?column?
----------
my.num
(1 row)
And, using a custom option without ''
and current_setting()
gets the error as shown below:
postgres=# SELECT my.num;
ERROR: missing FROM-clause entry for table "my"
LINE 1: SELECT my.num;
Next for example, you can use \set to set the number 2
to num
as shown below. *You must use :
to access num
according to the doc otherwise there is error and num
is removed after logout:
postgres=# \set num 2
postgres=# SELECT :num;
?column?
----------
2
(1 row)
And, you can use \set
to set the text John Smith
with ''
to name
as shown below. *Don't use ""
for John Smith
because ""
is included in the text so the output is "John Smith"
and you must use ''
for name
otherwise there is error and name
is removed after logout:
postgres=# \set name 'John Smith'
postgres=# SELECT :'name';
?column?
------------
John Smith
(1 row)
Be careful, setting the text John Smith
without ''
to name
removes the space between John
and Smith
as shown below:
postgres=# \set name John Smith
postgres=# SELECT :'name';
?column?
-----------
JohnSmith
(1 row)
And, only using \set
can show all variables as shown below:
postgres=# \set
AUTOCOMMIT = 'on'
COMP_KEYWORD_CASE = 'preserve-upper'
...
name = 'John Smith'
num = '2'
And, you can use \unset to unset(remove) num
and name
as shown below:
postgres=# \unset num
postgres=# \unset name
In addition, you can use \echo to output num
as shown below:
postgres=# \set num 2
postgres=# \echo :num
2
And, you can use \echo to output name
as shown below. *Don't use ''
for name
because ''
is included in the text so the output is 'John Smith'
:
postgres=# \set name 'John Smith'
postgres=# \echo :name
John Smith