4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Djangoのcreatesuperuserで「AssertionError: database connection isn't set to UTC」発生

Last updated at Posted at 2021-08-28

##概要
Django + psycopg2 で posgreSQL に接続しており、createsuperuser を実行すると「AssertionError: database connection isn't set to UTC」が発生し、スーパーユーザーが作成できない。

$ python manage.py createsuperuser
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Python\Python38\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "C:\Python\Python38\lib\site-packages\django\core\management\__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python\Python38\lib\site-packages\django\core\management\base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Python\Python38\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 79, in execute
    return super().execute(*args, **options)
  File "C:\Python\Python38\lib\site-packages\django\core\management\base.py", line 368, in execute
    self.check_migrations()
  File "C:\Python\Python38\lib\site-packages\django\core\management\base.py", line 458, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "C:\Python\Python38\lib\site-packages\django\db\migrations\executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "C:\Python\Python38\lib\site-packages\django\db\migrations\loader.py", line 49, in __init__
    self.build_graph()
  File "C:\Python\Python38\lib\site-packages\django\db\migrations\loader.py", line 212, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "C:\Python\Python38\lib\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 "C:\Python\Python38\lib\site-packages\django\db\models\query.py", line 276, in __iter__
    self._fetch_all()
  File "C:\Python\Python38\lib\site-packages\django\db\models\query.py", line 1261, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "C:\Python\Python38\lib\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 "C:\Python\Python38\lib\site-packages\django\db\models\sql\compiler.py", line 1184, in execute_sql
    return list(result)
  File "C:\Python\Python38\lib\site-packages\django\db\models\sql\compiler.py", line 1583, in cursor_iter
    for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):
  File "C:\Python\Python38\lib\site-packages\django\db\models\sql\compiler.py", line 1583, in <lambda>
    for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):
  File "C:\Python\Python38\lib\site-packages\django\db\utils.py", line 97, in inner
    return func(*args, **kwargs)
  File "C:\Python\Python38\lib\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

##結論
psycopg2のバージョンが原因らしい。
現在インストールされているバージョンは2.9.1だった。

pip show psycopg2
Name: psycopg2
Version: 2.9.1
Summary: psycopg2 - Python-PostgreSQL Database Adapter
Home-page: https://psycopg.org/
Author: Federico Di Gregorio
Author-email: fog@initd.org
License: LGPL with exceptions
Location: c:\python\python38\lib\site-packages
Requires:
Required-by:

psycopg2を一度アンインストールし、バージョン2.8.6を指定して再インストール。

$ pip uninstall psycopg2
Found existing installation: psycopg2 2.8.6
Uninstalling psycopg2-2.9.1:
  Would remove:
    c:\python\python38\lib\site-packages\psycopg2-2.9.1.dist-info\*
    c:\python\python38\lib\site-packages\psycopg2\*
Proceed (y/n)? y
  Successfully uninstalled psycopg2-2.9.1

$ pip install psycopg2==2.8.6
WARNING: Value for scheme.headers does not match. Please report this to <https://github.com/pypa/pip/issues/9617>
distutils: c:\python\python38\Include\UNKNOWN
sysconfig: c:\python\python38\Include
WARNING: Additional context:
user = False
home = None
root = None
prefix = None
Collecting psycopg2==2.8.6
  Downloading psycopg2-2.8.6-cp38-cp38-win_amd64.whl (1.1 MB)
     |████████████████████████████████| 1.1 MB 6.4 MB/s
Installing collected packages: psycopg2
WARNING: Value for scheme.headers does not match. Please report this to <https://github.com/pypa/pip/issues/9617>
distutils: c:\python\python38\Include\UNKNOWN
sysconfig: c:\python\python38\Include
WARNING: Additional context:
user = False
home = None
root = None
prefix = None
Successfully installed psycopg2-2.8.6

インストールが完了したので、改めて createsuperuser を実行。

# python manage.py createsuperuser
ユーザー名 (leave blank to use '******'): admin
メールアドレス:
Password:
Password (again):
Superuser created successfully.

無事にスーパーユーザーを作成できました。

##参考
https://exerror.com/assertionerror-database-connection-isnt-set-to-utc/

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?