Quick revision
The short answer
A PostgreSQL timestamp column without time zone stores only a local date-time, so to save an Instant into it, the driver or Hibernate must turn the moment into a clock reading using some zone, and to read it back they use a zone again. If that zone comes from the session or the JVM and changes, the values shift. The correct fix is a timestamp with time zone (timestamptz) column, explicit Hibernate time zone settings, a fixed JVM and JDBC zone such as UTC, and a test that writes and reads around DST in a non-UTC session.
What to remember
In this answer, we saw that a timestamp column without time zone holds only a clock reading, so storing an Instant in it forces Hibernate, the driver or PostgreSQL to convert with some zone, and a changed session or JVM zone shifts the values. We fixed it by migrating to timestamptz with a USING clause that names the zone the old values were in, setting Hibernate's time zone options and the JVM zone deliberately, and adding a test that writes and reads around DST with a different session zone.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A PostgreSQL timestamp column without time zone stores only a local date-time, so to save an Instant into it, the driver or Hibernate must turn the moment into a clock reading using some zone, and to read it back they use a zone again. If that zone comes from the session or the JVM and changes, the values shift. The correct fix is a timestamp with time zone (timestamptz) column, explicit Hibernate time zone settings, a fixed JVM and JDBC zone such as UTC, and a test that writes and reads around DST in a non-UTC session.
Why this question is asked
This question looks like a configuration detail, but it is really about what a column type means. Teams hit it when a database setting changes or a service moves host, and suddenly every timestamp is hours off. Interviewers ask it to see whether we can follow a value from Java through Hibernate and the JDBC driver into PostgreSQL and back. In this answer, we will recap the two PostgreSQL timestamp types, reproduce the shift, trace where the hidden conversion happens, and then fix the column, the settings and the tests.
1A Quick Recap of PostgreSQL's Two Timestamp Types
PostgreSQL has two timestamp types with confusing names. timestamp without time zone, usually written just timestamp, stores a calendar date and a clock time and nothing else, much like Java's LocalDateTime. timestamp with time zone, written timestamptz, stores a moment: on input it converts the value to UTC, and on output it shows that moment in the session's zone. It does not keep the original zone; it keeps the exact instant.
The session zone is a per-connection setting called TimeZone. psql takes it from the server default, and the PostgreSQL JDBC driver sets it from the JVM's default zone when it connects. So two connections can easily disagree about it.
2Reproducing the Shift
We can see the problem in psql without any Java. We store the same value in both column types, then change the session zone and ask each column what moment it holds.
Notice that tstz still means 12:00 UTC; it only displays differently. The timestamp column holds 12:00 regardless of zone, so whenever something converts it to a moment, the answer depends on the session zone of the moment of reading. Change the zone and the moment moves. That is exactly the shift the team saw.
3Where the Hidden Conversion Happens
An Instant is a moment, and a timestamp column can only hold a clock reading. So between our entity and the column, some layer must pick a zone and convert. Depending on the Hibernate version, the JDBC type it chooses for Instant, and the settings, that layer is Hibernate (using hibernate.jdbc.time_zone or the JVM default zone), the JDBC driver (using the JVM default zone), or PostgreSQL itself when it casts a timestamptz parameter into a timestamp column (using the session TimeZone).
The exact path differs between versions, so check yours, but the principle is the same in all of them: the write uses one zone and the read uses one zone. If both are the same, values survive. If the session zone or JVM zone changes between write and read, or the writer and the reader are different programs, the stored readings are interpreted in a different zone and every value shifts by the difference.
6 messages in order. Play them, or step through them one at a time.
Every arrow that crosses into the timestamp column needs a zone. Mismatched zones shift the value.
4Fix 1: Store a Moment in timestamptz
The real fix is to make the column type match the Java type. An Instant is a moment, so it belongs in timestamptz, which stores a moment normalised to UTC. Recent Hibernate versions map Instant to timestamp with time zone by default when they generate the schema, but an existing hand-written schema may still say timestamp, which is how teams end up here.
The USING clause matters. It states which zone the existing readings were in. If the old values were written while the session was in Europe/Berlin, we must say Europe/Berlin there, or we would bake the shift into the migrated data.
5Fix 2: Make Hibernate and the JVM Deliberate
Next we remove the guesswork from the layers above. Hibernate 6 introduced settings for how it stores zoned and offset types, including hibernate.timezone.default_storage, and it has long supported hibernate.jdbc.time_zone, which sets the zone it uses when converting. The exact names and defaults change between Hibernate 6 and 7, so check your version and set them on purpose rather than relying on defaults.
We also fix the JVM's default zone at UTC, so the JDBC driver opens every session in UTC and any conversion we missed behaves the same on every host. Notice that this is a safety net, not the fix: with timestamptz, the stored value is already correct whatever the session zone.
6Fix 3: A Test That Would Have Caught It
The original bug passed every test because tests ran in the same zone as the code and the database. So the test must deliberately disagree: open the session in a zone that is not UTC and has DST, write an Instant close to a DST change, and read it back.
Run it with Testcontainers against a real PostgreSQL, not an in-memory database, and make sure the SET and the repository calls share one connection, for example inside one transaction, since SET TIME ZONE applies only to its own session.
7Follow-up questions
Why not keep the timestamp column and just store UTC by convention?
It can work, and some teams do it on databases without a good zoned type. But the convention lives only in people's heads and in configuration. Every tool, report and new service must know to treat the column as UTC, and a single client with a different session zone silently writes wrong values. timestamptz makes the meaning part of the schema, so correctness does not depend on everyone remembering.
Should we map OffsetDateTime instead of Instant?
Either works with timestamptz. Instant expresses most clearly that the field is a moment. OffsetDateTime is convenient because JDBC 4.2 drivers support it directly through setObject and getObject, but PostgreSQL will not keep the original offset; the value comes back in UTC or in the session zone, so code must not depend on the offset it wrote.
Does the same problem exist in MySQL?
Yes, in a similar form. MySQL's DATETIME behaves like a clock reading, and TIMESTAMP converts through the session time zone. The driver's connection time zone settings decide how Java values are converted, so we set them explicitly and test with a non-UTC session there too.
8Conclusion
In this answer, we saw that a timestamp column without time zone holds only a clock reading, so storing an Instant in it forces Hibernate, the driver or PostgreSQL to convert with some zone, and a changed session or JVM zone shifts the values. We fixed it by migrating to timestamptz with a USING clause that names the zone the old values were in, setting Hibernate's time zone options and the JVM zone deliberately, and adding a test that writes and reads around DST with a different session zone.
Open with
The column type is the root cause. A PostgreSQL timestamp without time zone stores a local date and time, like a LocalDateTime, but the entity field is an Instant, which is a moment.
Then, if they want more
So on every write some layer has to turn the moment into a clock reading using a zone, and on every read it turns it back using a zone. Depending on the Hibernate version and settings, that is Hibernate, the JDBC driver with the JVM default zone, or PostgreSQL with the session TimeZone.
When the session zone changes, the read uses a different zone than the write, and the values shift. The correct fix is to store moments in timestamptz, which normalises to UTC, migrating with ALTER COLUMN TYPE timestamptz USING the zone the old values were actually written in.
Then I would make the other layers deliberate: set hibernate.jdbc.time_zone and the Hibernate 6 zoned storage setting explicitly, checking the exact names for our version, and run the JVM with UTC so the driver's sessions are predictable.
Finally, I would add a Testcontainers test that writes instants around a DST change in a Europe/Berlin session and reads them back in another zone, because the original tests passed only since everything ran in one zone.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Hibernate 6
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "With JPA and PostgreSQL, a team maps an Instant field to a timestamp column (without...": https://scrivio-interview-library.netlify.app/java/date-and-time/24c9572374/