Skip to content Skip to sidebar Skip to footer

Django 1.9.2 AssertionError: Database Connection Isn't Set To UTC

I have setup 3 servers now with PostgreSQL and have so far not seen this issue. I am now setting up the first server which is not running on a danish server, and i start getting er

Solution 1:

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.


Solution 2:

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.


Solution 3:

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.


Solution 4:

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.


Solution 5:

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.


Post a Comment for "Django 1.9.2 AssertionError: Database Connection Isn't Set To UTC"