ARTICLE AD BOX
I’m using PolarDB for PostgreSQL 16 and a Java application with the PostgreSQL JDBC driver.
In my Java code, I run a simple query:
String query = "SELECT now();"; try { conn2 = driver2.connect(url2, info2); stmt2 = conn2.prepareStatement(query); rs2 = stmt2.executeQuery(); while (rs2.next()) { String result = rs2.getString(1); System.out.println("Query Result: " + result); } } catch (SQLException e) { System.out.println("Connection failed!"); e.printStackTrace(); } finally { try { if (rs1 != null) rs1.close(); if (rs2 != null) rs2.close(); if (stmt1 != null) stmt1.close(); if (stmt2 != null) stmt2.close(); if (conn1 != null) conn1.close(); if (conn2 != null) conn2.close(); System.out.println("Resources closed!"); } catch (SQLException e) { e.printStackTrace(); } }No matter how I change the database timezone setting, the result printed by this Java code is always in Asia/Shanghai.
What I see in psql
If I connect with psql:
youyou=> show timezone; TimeZone ---------- UTC (1 row) youyou=> select now(); now ------------------------------- 2026-02-25 02:17:23.056023+00 (1 row)and
youyou=> show timezone; TimeZone --------------- Asia/Shanghai (1 row) youyou=> select now(); now ------------------------------- 2026-02-25 10:18:05.671536+08 (1 row)psql behaves as expected.
What I see in Java
With the same database, when I run my Java code in both configurations (DB timezone = UTC, and DB timezone = Asia/Shanghai), I always get:
Connected! Query Result: 2026-02-25 10:19:03.137973+08 Resources closed! Connected! Query Result: 2026-02-25 10:19:51.019322+08 Resources closed!So via JDBC, SELECT now() always comes back as +08, even when show timezone in psql returns UTC.
My questions
Why is JDBC always giving me Asia/Shanghai time, even when the database timezone is set to UTC and psql shows UTC?
What is the recommended / elegant way to control this in application code? For example:
Should I set the session timezone explicitly after getting the connection?
Or should I pass a timezone parameter in the JDBC URL?
Any detailed explanation on how PostgreSQL / PolarDB + JDBC handle timezones, and how to get consistent UTC timestamps in my Java app, would be appreciated.
