初出時は「encode 関数 + バイナリフォーマットで COPY」しましたが、その後、もっと簡単に普通の COPY だけで済むと分かり修正・追記しました。(1 月 4 日)
昨年最後の記事で、「バイナリフォーマットで
copy ( select st_asPNG( st_asRaster( st_buffer( st_point(1, 5), 10 ), 150, 150, '8BUI' ) ) ) to program '"D:/AppsPortable/GitPortable/2.6.2/App/Git/usr/bin/xxd" -r -p > "R:/bytea_to_xxd.png"'(format binary) -- バイナリフォーマットは不要 ;


参考リンク、実行環境
- StackOverflow : How to download Postgres bytea column as file
- Qiita : bytea
型カラムに入った画像をコマンドラインのみで取り出す - opamp_sando's blog : PostgreSQL
に画像を放り込んで遊んだ日記
実行環境はdd
xxd コマンドは、dd と同様 Git Portable 2.6.2 に入ってた
dd
とりあえずコマンドラインヘルプを表示すると ↓ 参考リンクで使われている
$ xxd --help Usage: xxd [options] [infile [outfile]] or xxd -r [-s [-]offset] [-c cols] [-ps] [infile [outfile]] Options: -a toggle autoskip: A single '*' replaces nul-lines. Default off. -b binary digit dump (incompatible with -ps,-i,-r). Default hex. -c cols formatoctets per line. Default 16 (-i: 12, -ps: 30). -E show characters in EBCDIC. Default ASCII. -e little-endian dump (incompatible with -ps,-i,-r). -g number of octets per group in normal output. Default 2 (-e: 4). -h print this summary. -i output in C include file style. -l len stop after octets. -o off add to the displayed file position. -ps output in postscript plain hexdump style. -r reverse operation: convert (or patch) hexdump into binary. -r -s off revert with added to file positions found in hexdump. -s [+][-]seek start at bytes abs. (or +: rel.) infile offset. -u use upper case hex letters. -v show version: "xxd V1.10 27oct98 by Juergen Weigert".

COPY ... TO PROGRAM で渡すコマンド内で、リダイレクトが使えた
ddbytea出力時にhex形式でエンコード ← 無駄にバイナリで COPY してたのが原因 ^^;
この項は初出時のままで、その後無意味と分かった情報です。冒頭のクエリだけ見て下さい m(_)m(1 月 4 日)
参考リンクでは、bytea
ところが今回、出力設定が
select current_setting('bytea_output'); -- 出力形式はhexになってたけど copy ( select st_asPNG( st_asRaster( st_buffer( st_point(1, 5), 10 ), 150, 150, '8BUI' ) ) ) to program '"D:/AppsPortable/GitPortable/2.6.2/App/Git/usr/bin/xxd" -r -p > "R:/test.png"' (format binary); -- エンコードなしで出力すると上手くいかない


まとめ
- dd
コマンドの時は「ヘッダのサイズが変わったらどうするか」未解決だったけど、こちらはスッキリ。バイナリフォーマットで COPY する必要もないし、これ一択でいいと思います。 - でも
encode 関数で hex 形式にする手間があり。使う関数や環境にもよると思うけど。 - 年明け初の記事が「補足」
かつ大幅に訂正 というのは冴えないなぁ(-_-;) もっと調べてから書けばよかった。