AssertionError: database connection isn't set to UTC


Django Problem Overview

I have done server setup multiple times with the same settings but this time, I am seeing the error message. It is not even allowing to migrate the database.

System check identified no issues (0 silenced).
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/usr/lib/python3.9/", line 954, in _bootstrap_inner
  File "/usr/lib/python3.9/", line 892, in run
    self._target(*self._args, **self._kwargs)
  File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/utils/", line 53, in wrapper
    fn(*args, **kwargs)
  File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/core/management/commands/", line 120, in inner_run
  File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/core/management/", line 458, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/migrations/", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/migrations/", line 49, in __init__
  File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/migrations/", line 212, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/migrations/", line 77, in applied_migrations
    return {(, migration for migration in self.migration_qs}
  File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/models/", line 276, in __iter__
  File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/models/", line 1261, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/models/", line 57, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/models/sql/", line 1170, in execute_sql
    return list(result)
  File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/models/sql/", line 1569, in cursor_iter
    for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):
  File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/models/sql/", line 1569, in <lambda>
    for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):
  File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/", line 97, in inner
    return func(*args, **kwargs)
  File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/backends/postgresql/", line 6, in utc_tzinfo_factory
    raise AssertionError("database connection isn't set to UTC")
AssertionError: database connection isn't set to UTC

Here is my for timezone.



USE_I18N = True

USE_L10N = True

USE_TZ = True

OS: Ubuntu 21.04 Python Version : 3.9.5 Django Version: 3.0 PostgreSQL: 13.3

I have also gone through another question but did not find any solution. Is there anyone who can help me to get this done? I have multiple server setup with same code without changing anything and worked but this time it is not.

Django Solutions

Solution 1 - Django

A recent update to psycopg2 version 2.9 is causing this issue as explained in this GitHub issue:

>Psycopg 2.9 changed the value passed to tzinfo_factory from an int to a timedelta. Django 2.2 (possibly newer but I'm on 2.2) has a check for offset == 0 and since timedelta(0) != 0 it goes boom.

A current solution would be to downgrade psycopg2 (or psycopg2-binary if you are using the stand-alone package) below 2.9 (e.g. psycopg2>=2.8,<2.9) in your requirements file.

For instance you can downgrade to 2.8.6 using:

pip install psycopg2==2.8.6


pip install psycopg2-binary==2.8.6

If you're using poetry, you can do poetry add [email protected] to fix your version to 2.8.6.

psycopg2 release history

Solution 2 - Django

I solved this by upgrading Django instead of downgrading psycopg. I don't know which version solves the issue exactly, but 3.2 certainly does.

The accepted answer is out of date now and you should decide against downgrading if you have the option to upgrade Django instead.

Solution 3 - Django

I had the same problem and i fixed it by simply removing this line from my file

USE_TZ = True

Solution 4 - Django

This is what I am working to get this all working on Django 2.2.x (which is not compatible with psycopg2>=2.9.0:

brew install libpq --build-from-source
brew install openssl
brew link openssl
export LDFLAGS="-L/opt/homebrew/opt/[email protected]/lib -L/opt/homebrew/opt/libpq/lib"
export CPPFLAGS="-I/opt/homebrew/opt/[email protected]/include -I/opt/homebrew/opt/libpq/include"
echo 'export PATH="/opt/homebrew/opt/openssl@3/bin:$PATH"' >> ~/.zshrc
brew install postgres
pip install psycopg2==2.8.6

I am on BigSur on a M1 macbook.

Solution 5 - Django

Ensure you have activated your virtual environment if any case you have deactivated it (please ensure you are within the virtual environment)

to activate your virtual environment use this command: source name of the virtual env/bin/activate


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNarendra VishwakarmaView Question on Stackoverflow
Solution 1 - Djangot-payneView Answer on Stackoverflow
Solution 2 - DjangotheberziView Answer on Stackoverflow
Solution 3 - DjangoInmass IdbelkacemView Answer on Stackoverflow
Solution 4 - DjangoradtekView Answer on Stackoverflow
Solution 5 - DjangoOkinda DerrickView Answer on Stackoverflow