The solution is to use the TIMESTAMPTZ fields for these points in time. Something like “when the user was created” should never be stored in the TIMESTAMP field, because by default it does not contain TZ information. The JDBC driver processes them completely randomly. For example, consider the following:
Suppose your JVM is in the America / Los_Angeles zone when your database server is in UTC.
Then create the following table:
CREATE TABLE test ( id INTEGER, ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP, tswz TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP );
If you choose from PSQL:
INSERT INTO test(id) values(1);
You will get the same values for "ts" and "tswz". Both will be the current UTC time. However, if you are making an EXACT EXACT query with Java using the JDBC driver, then “ts” will be the current time in Los Angeles, and “tswz” will be the UTC time.
I do not know HOW the driver passes the JVM time zone to the server in this case, because by default we use the field on the server. They say that they do not set the time zone for the session, but they should. In any case, if you use the TIMESTAMPTZ field, you will get the same moments from any JVM, no matter what time zone it is in.
source share