[Django]-Django 1.9.2 AssertionError: database connection isn't set to UTC

12πŸ‘

βœ…

I encountered this same problem, also with a server that is normally running with the UTC+2 (in my case, Europe/Oslo).

It turned out that the system zoneinfo files on my server (Centos 7) were corrupted, which became evident in pg_timezone_names.

postgres=# select * from pg_timezone_names where name like 'UTC';
 name | abbrev | utc_offset | is_dst
------+--------+------------+--------
 UTC  | CEST   | 02:00:00   | t
(1 row)

After running yum update tzdata to update my server’s timezone files, and restarting the PostgreSQL server, the issue appears to be resolved.

postgres=# select * from pg_timezone_names where name like 'UTC';
 name | abbrev | utc_offset | is_dst
------+--------+------------+--------
 UTC  | UTC    | 00:00:00   | f
(1 row)

My guess it that I might previously have run cat /usr/share/zoneinfo/Europe/Oslo > /etc/localtime without first removing /etc/localtime to change the timezone on the system, effectively overwriting the zoneinfo for UTC with the zoneinfo for Europe/Oslo.

πŸ‘€kiloreven

50πŸ‘

For 18/06/2021 this is a known issue, which comes from the psycopg-2/psycopg2-binary packages. Fortunately, they already have an open issue on their GitHub page, so hopefully it gets fixed soon.

As a temporary fix, you can pin the version in your requirements.txt to psycopg2-binary>=2.8,<2.9 or psycopg2>=2.8,<2.9, depending on what package you are using.

Thanks to @vinkomlacic for mentioning this in comments.

πŸ‘€defined-user

15πŸ‘

Here’s a simple workaround that I found to fix the issue for me.
That is to set:

USE_TZ = False

This helps to solve the issue.
By default it is set to True, so that gives the error for me when I use PostgreSql on Heroku. Maybe some synchronization happens between Django engine and the database.

πŸ‘€projjal

6πŸ‘

If you are using psycopg2 2.9, downgrade to psycopg2==2.8.6 or pip3 install psycopg2-binary==2.8.6.
This issue is caused by a recent update to psycopg2, version 2.9.

πŸ‘€Elijah Baraza

1πŸ‘

Thanks very much for the answer KFH, it helped me a lot. In case you are using Ubuntu, the following should fix it:

sudo apt install tzdata --reinstall

The problem was the same for me, as I had previously run sudo cp /usr/share/zoneinfo/America/Montreal /etc/localtime which messed with my postgres db. Re-installing tzdata and restarting postgres fixed the problem for me.

πŸ‘€Sam Creamer

0πŸ‘

The error got solved for me on windows by installing both: psycopg2==2.8.6 and psycopg2-binary==2.8.6 and uninstalling the latest version of any of both that were installed.

πŸ‘€rahul rachh

Leave a comment