3

I have an mobile application that make a insert in a Postgres database via JDBC. In the database table, i have a timestamp column and I need to insert the real timestamp of the server, to ensure that no one is inserting a fake date/time. It will be something like:

INSERT INTO some_table VALUES(...,now());

This code seems right, but if the mobile device have a different timezone, this difference reflect in the now() function. Example:

Date/time of server: 00:00:00 -04

Date/time of mobile: 00:12:34 +00

Result of insert above: 04:00:00

I already tried some other approaches, like:

INSERT INTO some_table VALUES(...,now()::timestamp)
INSERT INTO some_table VALUES(...,current_timestamp)
  • This seems to be a bug in the driver that sets the time zone always to the client's time zone. –  Nov 21 '17 at 13:10

1 Answers1

1

It'd make sense if you use the UTC timestamp to avoid having the timezone constantly in the middle. Let the client be concerned about the conversion to the current timezone.

Try it like this:

INSERT INTO some_table VALUES(..., now() at time zone 'utc')

Source: Using current time in UTC as default value in PostgreSQL

Stefano
  • 4,730
  • 1
  • 20
  • 28