[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
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
```

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
--
Ticket URL: <https://code.djangoproject.com/ticket/35240>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 20, 2024, 11:41:34 AM2/20/24
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 | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
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
}}}

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

--
--
Ticket URL: <https://code.djangoproject.com/ticket/35240#comment:1>

Django

unread,
Feb 20, 2024, 11:42:30 AM2/20/24
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: 4.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Fabi):

* version: 5.0 => 4.2

--
Ticket URL: <https://code.djangoproject.com/ticket/35240#comment:2>

Django

unread,
Feb 20, 2024, 2:16:05 PM2/20/24
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: 4.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Natalia Bidart):

* cc: Mariusz Felisiak, Florian Apolloner, Simon Charette (added)

Comment:

Hello Fabi, thank you for your report.

Could you please provide details about your settings for `DATABASES`? From
[https://docs.djangoproject.com/en/5.0/ref/settings/#time-zone these
docs]:

> 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
to django-...@googlegroups.com
#35240: Django doesn't set Postgres timezone to UTC when using psycopg3
-------------------------------+--------------------------------------
Reporter: Fabi | Owner: nobody
Type: Bug | Status: closed
Component: Uncategorized | Version: 4.2
Severity: Normal | Resolution: needsinfo
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Natalia Bidart):

* resolution: => needsinfo
* status: new => closed

--
Ticket URL: <https://code.djangoproject.com/ticket/35240#comment:4>

Django

unread,
Feb 20, 2024, 5:34:16 PM2/20/24
to django-...@googlegroups.com
#35240: Django doesn't set Postgres timezone to UTC when using psycopg3
-------------------------------+--------------------------------------
Reporter: Fabi | Owner: nobody
Type: Bug | Status: closed
Component: Uncategorized | Version: 4.2
Severity: Normal | Resolution: needsinfo
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Comment (by Fabi):

`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.
--
Ticket URL: <https://code.djangoproject.com/ticket/35240#comment:5>

Django

unread,
Feb 21, 2024, 9:02:40 AM2/21/24
to django-...@googlegroups.com
#35240: Django doesn't set Postgres timezone to UTC when using psycopg3
-------------------------------+--------------------------------------
Reporter: Fabi | Owner: nobody
Type: Bug | Status: closed
Component: Uncategorized | Version: 4.2
Severity: Normal | Resolution: needsinfo
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Comment (by David Sanders):

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
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35240#comment:6>
Reply all
Reply to author
Forward
0 new messages