You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django连接PostgreSQL执行makemigrations时出现UTF-8解码错误

Django + PostgreSQL 连接时 UnicodeDecodeError 解决方法

问题场景

使用Windows 11 Pro和Visual Studio Code,在pgAdmin 4(PostgreSQL 16)中创建新数据库后,Django项目settings.py配置如下:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'postgres',
        'USER': 'user',
        'PASSWORD': 'password',
        'HOST': 'localhost',
        'PORT': '5433',
    },
}

执行python manage.py makemigrations命令后,终端抛出以下错误:

Traceback (most recent call last):
  File "C:\Users\twitchtest\Myproject\project\manage.py", line 22, in <module>
    main()
  File "C:\Users\twitchtest\Myproject\project\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\twitchtest\Myproject\project\venv\Lib\site-packages\django\core\management\__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "C:\Users\twitchtest\Myproject\project\venv\Lib\site-packages\django\core\management\__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\twitchtest\Myproject\project\venv\Lib\site-packages\django\core\management\base.py", line 412, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\twitchtest\Myproject\project\venv\Lib\site-packages\django\core\management\base.py", line 458, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\twitchtest\Myproject\project\venv\Lib\site-packages\django\core\management\base.py", line 106, in wrapper
    res = handle_func(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\twitchtest\Myproject\project\venv\Lib\site-packages\django\core\management\commands\makemigrations.py", line 156, in handle
    loader.check_consistent_history(connection)
  File "C:\Users\twitchtest\Myproject\project\venv\Lib\site-packages\django\db\migrations\loader.py", line 313, in check_consistent_history
    applied = recorder.applied_migrations()
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\twitchtest\Myproject\project\venv\Lib\site-packages\django\db\migrations\recorder.py", line 81, in applied_migrations   
    if self.has_table():
       ^^^^^^^^^^^^^^^^
  File "C:\Users\twitchtest\Myproject\project\venv\Lib\site-packages\django\db\migrations\recorder.py", line 57, in has_table
    with self.connection.cursor() as cursor:
         ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\twitchtest\Myproject\project\venv\Lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\twitchtest\Myproject\project\venv\Lib\site-packages\django\db\backends\base\base.py", line 330, in cursor
    return self._cursor()
           ^^^^^^^^^^^^^^
  File "C:\Users\twitchtest\Myproject\project\venv\Lib\site-packages\django\db\backends\base\base.py", line 306, in _cursor
    self.ensure_connection()
  File "C:\Users\twitchtest\Myproject\project\venv\Lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\twitchtest\Myproject\project\venv\Lib\site-packages\django\db\backends\base\base.py", line 289, in ensure_connection    
    self.connect()
  File "C:\Users\twitchtest\Myproject\project\venv\Lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\twitchtest\Myproject\project\venv\Lib\site-packages\django\db\backends\base\base.py", line 270, in connect
    self.connection = self.get_new_connection(conn_params)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\twitchtest\Myproject\project\venv\Lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\twitchtest\Myproject\project\venv\Lib\site-packages\django\db\backends\postgresql\base.py", line 275, in get_new_connection
    connection = self.Database.connect(**conn_params)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\twitchtest\Myproject\project\venv\Lib\site-packages\psycopg2\__init__.py", line 122, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc2 in position 61: invalid continuation byte

已确认计算机名称为英文。

解决方法

  • 检查数据库配置参数的编码
    确保settings.py中的PASSWORD等参数没有非UTF-8编码的特殊字符,可暂时替换为纯英文数字的密码测试;同时确认settings.py文件本身以UTF-8编码保存。

  • 验证PostgreSQL数据库编码
    在pgAdmin中右键目标数据库 → 属性 → 定义,确认编码为UTF8。如果不是,删除现有数据库,重新创建时指定UTF-8编码。

  • 更换psycopg2版本
    卸载当前psycopg2,安装二进制兼容版本:

    pip uninstall psycopg2
    pip install psycopg2-binary
    

    若仍有问题,可指定稳定版本安装,例如:

    pip install psycopg2-binary==2.9.9
    
  • 切换终端编码为UTF-8
    在Windows终端中执行以下命令切换编码后,再运行Django命令:

    chcp 65001
    python manage.py makemigrations
    

内容的提问来源于stack exchange,提问作者twitchtest

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 20:18:10