Reputation: 103
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)
Upvotes: 3
Views: 2195
Reputation: 5076
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
Upvotes: 1