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)
1
  • This seems to be a bug in the driver that sets the time zone always to the client's time zone. Commented Nov 21, 2017 at 13:10

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.