After adding the HOST setting, I get a utf-8 error when creating a superuser

I am trying to open my project with a postgresql database in docker. There is no problem in the build process, but when I try to create a superuser, I get a utf-8 error.

settings:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'firstpro',
        'USER': 'postgres',
        'PASSWORD': 'postgres',
        'HOST': 'db',
        'PORT': '5432',
    }
}

yml file

services:
  app:
    build: .
    volumes:
      - .:/myproject
    ports:
      - "8000:8000"
    image: app:django
    container_name: django_container
    command: python manage.py runserver 0.0.0.0:8000
    depends_on:
      - db
  db:
    image: postgres
    volumes:
      - postgres_data:/var/lib/postgresql/data
    ports:
      - '5432:5432'
    environment:
      - POSTGRES_DB=firstpro
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    container_name: postgres_db

volumes:
  postgres_data: {}

dockerfile

FROM python:3.12.7

ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

RUN apt-get update \
    && apt-get install -y --no-install-recommends \
    libpq-dev build-essential

WORKDIR /myproject

COPY requirements.txt /myproject/

RUN pip install -r requirements.txt

COPY . .

error

PS D:\django code\firstp\firstpr> python .\manage.py createsuperuser
Traceback (most recent call last):
  File "D:\django code\firstp\firstpr\manage.py", line 22, in <module>
    main()
  File "D:\django code\firstp\firstpr\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\MONSTER\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\core\management\__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "C:\Users\MONSTER\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\core\management\__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\MONSTER\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\core\management\base.py", line 412, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\MONSTER\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 88, in execute
    return super().execute(*args, **options)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MONSTER\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\core\management\base.py", line 457, in execute
    self.check_migrations()
  File "C:\Users\MONSTER\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\core\management\base.py", line 574, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MONSTER\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\db\migrations\executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MONSTER\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\db\migrations\loader.py", line 58, in __init__
    self.build_graph()
  File "C:\Users\MONSTER\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\db\migrations\loader.py", line 235, in build_graph
    self.applied_migrations = recorder.applied_migrations()
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MONSTER\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\db\migrations\recorder.py", line 81, in applied_migrations
    if self.has_table():
       ^^^^^^^^^^^^^^^^
  File "C:\Users\MONSTER\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\db\migrations\recorder.py", line 57, in has_table
    with self.connection.cursor() as cursor:
         ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MONSTER\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MONSTER\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\db\backends\base\base.py", line 330, in cursor
    return self._cursor()
           ^^^^^^^^^^^^^^
  File "C:\Users\MONSTER\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\db\backends\base\base.py", line 306, in _cursor
    self.ensure_connection()
  File "C:\Users\MONSTER\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MONSTER\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\db\backends\base\base.py", line 289, in ensure_connection
    self.connect()
  File "C:\Users\MONSTER\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MONSTER\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\db\backends\base\base.py", line 270, in connect
    self.connection = self.get_new_connection(conn_params)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MONSTER\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MONSTER\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\db\backends\postgresql\base.py", line 269, in get_new_connection
    connection = self.Database.connect(**conn_params)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MONSTER\AppData\Local\Programs\Python\Python312\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 0xf6 in position 56: invalid start byte

Interesting. Are you completely sure that there are no hidden UTF-8 special control characters? Please view the settings file in hex editor to be sure. Also Python, Django, psycopg versions would help.

I’m creating a new project and I’m not using any special characters in the settings file. My Python version is 3.12.7, Django version is 4.2 and psycopg2-binary version 2.9.9

I would use the debugger to see what are the parameters to connect in line 122 of psycopg2/init.py.

Can you edit that file? just type “import pdb; pdb.set_trace()” in a line before that 122 line and you will drop into pdb debugger; you can see the variables that the _connect function is being called with.

Nicer way to set breakpoints would be – https://stackoverflow.com/a/6980836/401516 . Or, if you’re using PyCharm, you can set breakpoints in GUI and run the code in debug mode.

0xf6 is ö in ASCII/Windows/latin-1, but invalid in utf8.