#35240 closed Bug (needsinfo)
Django doesn't set Postgres timezone to UTC when using psycopg3
Reported by: | Fabi | Owned by: | nobody |
---|---|---|---|
Component: | Uncategorized | Version: | 4.2 |
Severity: | Normal | Keywords: | |
Cc: | Fabi, Mariusz Felisiak, Florian Apolloner, Simon Charette | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
Configuration uses USE_TZ=True
and database defaults to America/New_York
. I would expect the connection then being set to UTC, but it is not changed:
In [4]: connections["default"].cursor().connection.info.timezone Out[4]: zoneinfo.ZoneInfo(key='America/New_York')
The wrapper and the psycopg3 timezone adapter both think its UTC though:
In [5]: connections["default"].timezone Out[5]: datetime.timezone.utc In [6]: connections["default"].cursor().connection.adapters.get_loader(TIMESTAMPTZ_OID, Format.TEXT).timezone Out[6]: datetime.timezone.utc
Looking at the code I think the problem is that the adapter gets initialized with self.timezone
which then gets compared with self.timezone
later again, resulting in a no-op:
Change History (6)
comment:1 by , 10 months ago
Description: | modified (diff) |
---|
comment:2 by , 10 months ago
Version: | 5.0 → 4.2 |
---|
comment:3 by , 10 months ago
Cc: | added |
---|
comment:4 by , 10 months ago
Resolution: | → needsinfo |
---|---|
Status: | new → closed |
comment:5 by , 10 months ago
DATABASES
does not have a TIME_ZONE
set.
While there might be other bugs in this specific code, I was able to fix this specific problem by defaulting the database to UTC.
I'll leave it up to you if you want to close this or leave it open for further investigation.
comment:6 by , 10 months ago
Tested on current main, seems to work as expected 🤷♂️:
>>> settings.USE_TZ True >>> settings.TIME_ZONE 'Australia/Sydney' >>> pprint.pprint(settings.DATABASES['default']['TIME_ZONE']) None >>> connections['default'].cursor().connection.info.timezone datetime.timezone.utc
Hello Fabi, thank you for your report.
Could you please provide details about your settings for
DATABASES
? From these docs:Also, could you please try using latest Django 5.0 or from the
main
branch? Django 4.2 is not receiving bug fixes any more so we'd need to confirm is this is still an issue in newer/supported versions.In any case, it's unexpected (to me) that from the source links you provided,
get_adapters_template
takes two parametersuse_tz, timezone
but onlytimezone
is used.