RoR Postgresql timezone group by not working on Heroku

我的未来我决定 提交于 2019-12-14 03:47:00

问题


I would like my reports to display in CST while my database is in UTC. For this to work I need to be able to group by a time column and have the db do timezone conversions.

This works on my dev postgresql box:

    offset = -5
    result = ActiveRecord::Base.connection.execute("select  date(srl.created_at AT TIME
    ZONE '#{offset.to_s}') AS date, count(*) from server_request_logs srl where     
    srl.created_at between '#{@from.to_s(:db)}' and '#{@to.to_s(:db)}' group by 
    date(srl.created_at AT TIME ZONE '#{offset.to_s}')")

on heroku it errors with:

ActiveRecord::StatementInvalid: PGError: ERROR:  time zone "-5" not recognized

Its also not working with TZInfo names like 'America/Winnipeg' or supported timezones like 'CST' from http://www.postgresql.org/docs/7.2/static/timezones.html

has anyone been able to get this working?


回答1:


First off, make sure that you define your timestamp columns and variables as TIMESTAMP WITH TIME ZONE (or timestamptz for short). In PostgreSQL this doesn't actually cause any time stamp to be saved; but makes it a fixed point in time, stored in UTC. You can view it AT TIME ZONE of your choosing with clean semantics. TIMESTAMP WITHOUT TIME ZONE (which is what you get if you just say TIMESTAMP) is not a fixed point in time until it is resolved against a time zone, and is therefore much harder to work with.

The documentation page you cite regarding time zones is from a very old version of PostgreSQL which has gone out of support. Maybe this page will be of more help to you:

http://www.postgresql.org/docs/current/interactive/datetime-config-files.html



来源:https://stackoverflow.com/questions/10274375/ror-postgresql-timezone-group-by-not-working-on-heroku

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!