Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use GMT times in postgresql?

We are running about in our java code with GregorianCalendar. We would like to persist these in our postgresql database as GMT +0 time in timestamp type columns, but we're not sure how to go about it. Should the fields be with time stamp or without? By a similar token which "current" time postgres function should we be using current_timestamp or localtimestamp?

like image 909
Adam Avatar asked Sep 04 '09 17:09

Adam


People also ask

What timezone does PostgreSQL use?

PostgreSQL assumes your local time zone for any type containing only date or time. All timezone-aware dates and times are stored internally in UTC . They are converted to local time in the zone specified by the TimeZone configuration parameter before being displayed to the client.

How does Postgres store timestamp with timezone?

The TIMESTAMP (also known as TIMESTAMP WITHOUT TIME ZONE ) and TIMESTAMPTZ (also known as TIMESTAMP WITH TIME ZONE ) types stored as a 64-bit integer as a microsecond offset since 1970-01-01 in CRDB and as a 64-bit integer microsecond offset since 2000-01-01 in PostgreSQL (by default).

How do I get system time in PostgreSQL?

You can use the PostgreSQL Now() function to display the current date and time of the timezone (default or user-defined) without any timestamp. You can use the “timestamp” keyword along with the Now() function.


1 Answers

Start here - section 8.5.3. "Time Zones" from the documentation.

The difference between CURRENT_TIMESTAMP and LOCALTIMESTAMP is the type of the result - the former returns "timestamp with time zone" and the latter - "timestamp":

milen=> select CURRENT_TIMESTAMP;
              now
-------------------------------
 2009-09-05 01:21:37.595704+03
(1 row)

milen=> select LOCALTIMESTAMP;
         timestamp
----------------------------
 2009-09-05 01:21:41.956355
(1 row)

It's another matter entirely at which time zone you want to see the current time. In this case the default time zone (that could set in several ways, all described in the section linked above) is important. Also you could use "AT TIME ZONE" in a query to get the current time at specific time zone without "fiddling" with the "timezone" setting:

milen=> select CURRENT_TIMESTAMP AT TIME ZONE 'UTC';
          timezone
----------------------------
 2009-09-04 22:21:44.418236
(1 row)
like image 145
Milen A. Radev Avatar answered Sep 18 '22 05:09

Milen A. Radev