Whenever I run some kind of database operation (IE update_attributes, save etc.), timestamps are in UTC

177 views Asked by At

When I run:

psql great_dev -c 'show timezone'

The following is returned:

TimeZone  
------------
US/Eastern
(1 row)

I want the timezone to be EST, so I think that is correct. The problem is that whenever I run some kind of database operation (IE update_attributes, save etc.), the timestamps are in UTC like this:

SQL (0.6ms)  UPDATE "schedules" SET "accepted" = $1, "updated_at" = $2 WHERE 
 "schedules"."id" = 46  [["accepted", "only_freelancer"], 
 ["updated_at", Mon, 02 Dec 2013 16:51:07 UTC +00:00]]

I am starting to implement several background jobs and it is essential that I understand what timezone Postgres is in.

  1. Is Postgres in EST or UST?
  2. If it is in UST, how do I convert it to EST and is it dangerous to do so?
  3. I am going to allow a user to enter in timestamps from a select menu - should the menu list UTC times or EST times?
1

There are 1 answers

0
Erwin Brandstetter On BEST ANSWER

The default timestamp type in Postgres is timestamp without time zone.
To deal with this, employ the data type timestamp with time zone.

Or run your database with the time zone set to UTC and use timestamp [without time zone]. Be aware that the time zone setting of the session is relevant to what Postgres actually returns for your queries.

This related answer provides all the details you might need:
Ignoring timezones altogether in Rails and PostgreSQL