# select now() \gset # # \echo :now 2016-06-26 14:18:33.961906+09

↓ 変数の中身をクエリに使う例。数値なら「:
# select :'now'; +-------------------------------+ | ?column? | +-------------------------------+ | 2016-06-26 14:18:33.961906+09 | +-------------------------------+ (1 row) # select date :'now'; +------------+ | date | +------------+ | 2016-06-26 | +------------+ (1 row)

最初に書いたとおり、結果が
# select * from generate_series(1, 9) as vals \gset more than one row returned for \gset # select array_agg(vals) from generate_series(1, 9) as vals \gset # # \echo :array_agg {1,2,3,4,5,6,7,8,9}

↓ 列が複数あっても大丈夫。それぞれの列名がそのまま変数名になります。
# select chr(12354) as char1, chr(12436) as char2 \gset # # \echo :char1 あ # \echo :char2 ゔ

じゃあ、アドホックなクエリで同じ列名が複数ある場合は
# select chr(12354) as char, # chr(12436) as char, # now() # \gset # # \echo :char ゔ # \echo :now 2016-06-26 14:46:26.624577+09

psql
# \set AUTOCOMMIT = 'on' PROMPT1 = '# ' PROMPT2 = '# ' PROMPT3 = '>> ' ... ENCODING = 'UTF8' now = '2016-06-26 14:46:26.624577+09' array_agg = '{1,2,3,4,5,6,7,8,9}' char1 = 'あ' char2 = 'ゔ' char = 'ゔ'
最後。シェルの環境変数にするには ↓ メタコマンド
# \setenv now :now # # \! echo $now 2016-06-26 14:46:26.624577+09
このシェル環境変数は、psql