実行環境はCygwin 2.5.1 x64 + psql。昨日は\gsetコマンドを使ってクエリ結果を変数に入れたけど、今日は\gコマンドをパイプでputclipに。普通は画面に出る整形済みクエリ結果が、一発でクリップボードに入ります。逆にクリップボードの中身をpsqlコマンドに持ってくるには、バッククォートで`getclip`。


最初にCygwinputclip / getclipを使う準備。実はなくても/dev/clipboardを使えばよいと後で知りましたが
(参考: cygwin, linuxpbcopy / pbpaste)まぁ記述が簡単になるので良いかと^^;
$ which putclip
(not found. 自分のCygwinに入ってなかった)

$ apt-cyg searchall putclip
cygutils-extra
zsh
(two packages found. 今回はcygutils-extraを入れる)

$ apt-cyg install cygutils-extra
(completed.)

$ which putclip; which getclip
/usr/bin/putclip
/usr/bin/getclip
(ok.)

$ export | putclip
(test. exportの出力がクリップボードに入る)

$ echo あいうえお | iconv -f utf8 -t sjis | putclip
(test. Windowsのクリップボードに送るため文字コードを変換)

$ getclip | iconv -f sjis -t utf8
(test. 適当な文字をコピーしてから実行. 上と逆の文字コード変換)


putclip / getclipの準備ができたので、psqlから使ってみます。まずクエリ結果をクリップボードへ流し込み。
# \pset border 2
Border style is 2.
# select * from pg_tables \g | putclip


何も起きないけど、クリップボードをエディタ等にペーストすると ↓ 確かにクエリ結果が入ってました。
+--------------------+-------------------------+------------+------------+------------+----------+-------------+-------------+
|     schemaname     |        tablename        | tableowner | tablespace | hasindexes | hasrules | hastriggers | rowsecurity |
+--------------------+-------------------------+------------+------------+------------+----------+-------------+-------------+
| pg_catalog         | pg_statistic            | postgres   |            | t          | f        | f           | f           |
| pg_catalog         | pg_type                 | postgres   |            | t          | f        | f           | f           |
| pg_catalog         | pg_authid               | postgres   | pg_global  | t          | f        | f           | f           |
| pg_catalog         | pg_user_mapping         | postgres   |            | t          | f        | f           | f           |
| pg_catalog         | pg_largeobject          | postgres   |            | t          | f        | f           | f           |
...


日本語のクエリ結果のテスト。文字コード変換しないと、よくある「UTF-8をそのままSJISに突っ込んでしまった」形で化けます。
# select repeat(chr(12354), 10) \g | putclip

-- clipboard: 文字化け
+----------------------+
|        repeat        |
+----------------------+
| 縺ゅ≠縺ゅ≠縺ゅ≠縺ゅ≠縺ゅ≠ |
+----------------------+
(1 row)

# select repeat(chr(12354), 10) \g | iconv -f utf8 -t sjis | putclip

-- clipboard: OK
+----------------------+
|        repeat        |
+----------------------+
| ああああああああああ |
+----------------------+
(1 row)


\gコマンドの後が長くて使いにくいなぁ~。なのでコマンドごと好きな変数名で記憶させます。これを.psqlrcに書いとけば、次回以降も簡単。
# \set put '\\g | iconv -f utf8 -t sjis | putclip'
# select repeat(chr(12454), 10) :put

-- clipboard:
+----------------------+
|        repeat        |
+----------------------+
| ウウウウウウウウウウ |
+----------------------+
(1 row)


逆に、クリップボードの内容をpsql上に持ってくるにはgetclipを使用。文字コードを先ほどと逆に(SJIS => UTF8)変換してバッククォートで囲みます。単に表示するならメタコマンド\echoで。クエリに使うには、いったん適当な変数に入れて。
(クリップボードに適当な文字列をコピー)

-- コピーした文字列を表示
# \echo `getclip | iconv -f sjis -t utf8`

-- コピーした文字列を変数に入れ、クエリに使う
# \set str `getclip | iconv -f sjis -t utf8`
# select text :'str';


よく考えるとgetclipは、psql上でペーストするのと同じ。その方が文字コードが自動変換されるし便利(少なくとも自分のCygwin + ConEmuでは)。だから余り使わないかも。一方putclipはいろいろ使い途がありそう。最後に、両方を一つにまとめ。冒頭の画像もこれです。
-- 出力をクエリ結果だけに
\t on \\ \pset format unaligned

-- クエリ実行し、結果を直接クリップボードへ
select array_agg(chr(12353 + i) order by random())
from generate_series(1, 20) as i
\g | iconv -f utf8 -t sjis | putclip

-- クリップボードを変数に
# \set str `getclip | iconv -f sjis -t utf8`

-- クリップボードの中身を表示
# \echo :str
{い,か,さ,あ,ぎ,き,ぉ,が,え,ぐ,お,う,ぃ,ご,こ,げ,ぇ,ぅ,け,く}

-- その変数をクエリに使う
# select unnest(:'str' :: text[]);
い
か
さ
あ
ぎ
き
...