Python has given an opportunity to programmers all over the world to design web applications, web pages, and other applications that create a difference in the world of programming. Some apps are so amazing that users love to use and millions of users are using such apps. Applications that are user-friendly shows an outstanding response. Python is always more fun when using libraries and frameworks. For using these, the basic requirement is to understand the concept of Python. Django is a Python backend server-side framework that is used to encourage clean, maintain, secure, and rapid development of websites. Coding with Django is quite simpler as you can simply write the code. When working with Django, you can face the error “AssertionError: database connection isn’t set to UTC”.
Not to worry when you get this AssertionError as we can help you drag you out of it so that you can code your program perfectly. We at Tutopal always enjoy helping you solve the error. First, let’s figure out how the error shows up
How the error occurs
When you try to use PostgreSQL (the database) on the server, you get the following error
System check identified no issues (0 silenced).
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/usr/lib/python3.9/threading.py", line 954, in _bootstrap_inner
self.run()
File "/usr/lib/python3.9/threading.py", line 892, in run
self._target(*self._args, **self._kwargs)
File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 120, in inner_run
self.check_migrations()
File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/core/management/base.py", 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/executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/migrations/loader.py", line 49, in __init__
self.build_graph()
File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/migrations/loader.py", 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/recorder.py", line 77, in applied_migrations
return {(migration.app, migration.name): migration for migration in self.migration_qs}
File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/models/query.py", line 276, in __iter__
self._fetch_all()
File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/models/query.py", 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/query.py", 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/compiler.py", line 1170, in execute_sql
return list(result)
File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/models/sql/compiler.py", 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/compiler.py", 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/utils.py", line 97, in inner
return func(*args, **kwargs)
File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/backends/postgresql/utils.py", line 6, in utc_tzinfo_factory
raise AssertionError("database connection isn't set to UTC")
AssertionError: database connection isn't set to UTC
It happens when you use the following timezone settings.py
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
Ways To Fix the Error Warning “AssertionError: database connection isn’t set to UTC”
We have come up with a few ways to help you remove the error.
Solution 1 – Downgrade psycopg2 to 2.8.6
The effective way to fix the error is to simply downgrade the psycopg2 version. You need to downgrade the version to 2.8.6. To do that, you can follow the below command
pip install psycopg2==2.8.6
or
pip install psycopg2-binary==2.8.6
Solution 2 – Delete this line
This one is the simplest way to handle the error as you just need to delete a line from your settings.py. Remove the following line
USE_TZ = True
It effectively makes the error go away.
Solution 3 –Work on the Django 2.2.x
In this solution, you can simply work on Django 2.2.x, which is incompatible with psycopg2>=2.9.0. to make it work, you need to follow the below code
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
It resolves the error for most macOS and MacBook users. You can still try this.
Conclusion
We shed light on the solutions to help you fix the error message “AssertionError: database connection isn’t set to UTC” in an efficient way. You can try any of the solutions to resolve the issue.
I hope it helps!