RoR Postgresql时区组不在Heroku上工作

我希望我的报告在CST中显示,而我的数据库是UTC。 为此,我需要能够按时间列进行分组并让db进行时区转换。

这适用于我的dev postgresql框:

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}')") 

在heroku上它出错了:

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

它也没有使用像’America / Winnipeg’这样的TZInfo名称或者来自http://www.postgresql.org/docs/7.2/static/timezones.html的 ‘CST’支持的时区。

有没有人能够使这个工作?

首先,确保将时间戳列和变量定义为TIMESTAMP WITH TIME ZONE (或简称timestamptz )。 在PostgreSQL中,这实际上并不会导致保存任何时间戳; 但使其成为一个固定的时间点,以UTC格式存储。 您可以使用干净的语义在您选择的AT TIME ZONE进行查看。 TIMESTAMP WITHOUT TIME ZONE (如果你只是说TIMESTAMP就是你得到的),它不是一个固定的时间点,直到它被解决为时区,因此更难以使用。

您引用时区的文档页面来自PostgreSQL的一个非常旧的版本,它已经不再支持了。 也许这个页面对您有所帮助:

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