I have a postgres database, that stores some accounting data. The accounting data have a timestamp (with timezone).
I would like to aggregate/group the accounting data by date. But the date should be in the user's/requested timezone not UTC.
SELECT '2023-08-01'::timestamp AT TIME ZONE 'Europe/Berlin'; -- 2023-07-31 22:00:00+00
Outputs the correct UTC value for the timestamp,
but if I cast that to date then it returns the UTC date:
SELECT date('2023-08-01'::timestamp AT TIME ZONE 'Europe/Berlin'); -- 2023-07-31
Is there a way to output the date in Europe/Berlin time? (2023-08-01)
show timezone;return?UTC.SELECT date('2023-08-01'::timestamptz AT TIME ZONE 'Europe/Berlin'); 2023-08-01.SET TIME ZONE 'Europe/Berlin'fixes the issue as well.timestamp with time zonefield doesn't actually store the time zone. So users with different time zone settings will get different dates back with that approach. For this reason, often it's useful to store an additional field that is adatetype for the date relevant to the business logic, along side the timestamp. Especially for accounting - you probably don't want end-of-month dates being treated as start-of-month dates in other time zones.