[Django] #35240: Django doesn't set Postgres timezone to UTC when using psycopg3
26 views
Skip to first unread message
Django
unread,
Feb 20, 2024, 11:40:39 AM2/20/24
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to django-...@googlegroups.com
#35240: Django doesn't set Postgres timezone to UTC when using psycopg3
-----------------------------------------+------------------------
Reporter: Fabi | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 5.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
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
```
-------------------------------+--------------------------------------
Description changed by Fabi:
Old description:
> 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:
> -
> https://github.com/django/django/blob/31314980be428c0ab8a6cf40cce0a0c17ead8ec6/django/db/backends/postgresql/base.py#L243-L246 > -
> https://github.com/django/django/blob/31314980be428c0ab8a6cf40cce0a0c17ead8ec6/django/db/backends/postgresql/base.py#L349-L354
New description:
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
}}}
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to django-...@googlegroups.com
#35240: Django doesn't set Postgres timezone to UTC when using psycopg3
-------------------------------+--------------------------------------
Reporter: Fabi | Owner: nobody
Type: Bug | Status: new
> TIME_ZONE
> Default: None
> A string representing the time zone for this database connection or None
[...]
> When USE_TZ is True, reading datetimes from the database returns aware
datetimes with the timezone set to this option’s value if not None, or to
UTC otherwise.
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 parameters `use_tz, timezone`
but only `timezone` is used.
--
Ticket URL: <https://code.djangoproject.com/ticket/35240#comment:3>
Django
unread,
Feb 20, 2024, 2:16:20 PM2/20/24
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to django-...@googlegroups.com
#35240: Django doesn't set Postgres timezone to UTC when using psycopg3
-------------------------------+--------------------------------------
Reporter: Fabi | Owner: nobody