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

322πŸ‘

The release of psycopg2 version 2.9 caused this error as explained in this GitHub issue:

https://github.com/psycopg/psycopg2/issues/1293#issuecomment-862835147

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.

One solution is 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

or

pip install psycopg2-binary==2.8.6

If you’re using poetry, you can do poetry add psycopg2@2.8.6 to fix your version to 2.8.6.

psycopg2 release history

πŸ‘€t-payne

22πŸ‘

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

USE_TZ = True

15πŸ‘

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.

πŸ‘€theberzi

11πŸ‘

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/openssl@1.1/lib -L/opt/homebrew/opt/libpq/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openssl@1.1/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.

πŸ‘€radtek

2πŸ‘

For ubuntu users, installing psycopg2==2.8.6, psycopg2-binary==2.8.6 with older versions of django may raise some kind of error. To solve that issue, try,

sudo apt-get install gcc libpq-dev -y

sudo apt-get install python3-dev python3-pip python3-venv python3-wheel -y

pip3 install wheel

This is one kind of solution, other solutions are available at the link below.
https://exerror.com/error-invalid-command-bdist_wheel/#:%7E:text=Just%20use%20below%20command%20pip,command%20python%20setup.py%20bdist_wheel.&text=invalid%20command%20%27bdist_wheel%27-,To%20Solve%20error%3A%20invalid%20command%20%27bdist_wheel%27%20Error%20You%20just,command%20python%20setup.py%20bdist_wheel.?

0πŸ‘

In settings.py
I have changed the value of USE_TZ = to False It fixed the problem for me.

[Changing the version of psycobg2 didn’t work for me. Swithing it to older version introduced a new problem of breaking the live development server.]
[It is Windows 11]

πŸ‘€Ahmad Ali

-1πŸ‘

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

-1πŸ‘

I had this issue..solved by updating django latest version.

INSTALLED_APPS = [
    'cart',
]

this line caused me error when lower version (2.2) is used
u can also solve this issue by changing the above line of code to

INSTALLED_APPS = [
        'cart.apps.CartConfig',   
]

CartConfig is the class name u can copy and paste from apps.py of the same app

πŸ‘€haseena mk

-1πŸ‘

Author Admin,

ubuntu desktop Ubuntu 22.04.3 LTS

Change in setting.py in your django
USE_TZ = False, you’re indicating that PostgreSQL will handle time zone conversions, so Django won’t interfere with it.

my ubuntu setting in pycharm django

πŸ‘€Admin

Leave a comment