0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PostgresqlのTimezoneの設定ミスで1ヶ月もずれちゃった話

Last updated at Posted at 2020-11-20

Timestamp型の集計日sum_dtに、「毎月1日 00:00:00」の日時を入れて、取得時にYYYY/MMに変換して使用している処理があるのですが。

SELECT to_char(sum_dt,'YYYY/MM')::TEXT
FROM monthly_data

RDSのスナップショットを復元して別の環境を立ち上げたところ、なぜか取れるデータが1ヶ月ずれることに。

DBeaverで見る分にはデータ問題なさそうなのに・・・。

と思ったら、パラメータグループを指定するのを忘れていて、TimezoneがデフォルトのUTCになっていました。

2020/11/01 00:00:00 +0900 (JST)は
2020/10/31 15:00:00 +0000 (UTC)と同値なので、冒頭のクエリだと1ヶ月の違いになってしまっていました。

DBeaverで見る時はいい感じにJSTに変換してくれていたようです。

日時で出していれば、9時間ずれに気がついて、Timezoneの問題とすぐ気がついたと思うのですが、1ヶ月ずれていると思い込んでいて発見まで1時間かかりましたw。

そもそも時間が必要ないのにTimestamp型のカラムにするのよくないという話。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?