ツー

日常の記録

PostgreSQLで単発で結果が必要なときに使うSQL文たち

timezoneの設定

timezoneの変え方をいつも忘れるが、SET SESSION timezone TO 'Asia/Tokyo'; でtimezoneが変わってtimestampの表示も変わる。

postgres=> select to_timestamp(1234567890);
      to_timestamp
------------------------
 2009-02-13 23:31:30+00
(1 row)
SET SESSION timezone TO 'Asia/Tokyo';
postgres=> select to_timestamp(1234567890);
      to_timestamp
------------------------
 2009-02-14 08:31:30+09
(1 row)

epochからformatしたdate string

上記にもある通り、timezoneで値が変わるから表示前にtimezoneを把握してから出力したほうがいい。

show timezone;
postgres=> select to_char(to_timestamp(1234567890), 'YYYY/MM/DD HH24:MI:SS');
       to_char
---------------------
 2009/02/14 08:31:30