skip to Main Content

I can not connect Django to PostgreSQL database. It show me an encoding problem:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb3 in position 86: invalid start byte

The problem is in that line:

  File "C:UsersRoboDesktopStockvenvLibsite-packagespsycopg2__init__.py", line 122, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)

I have tried to change encoding in Settings option, but with no results.

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": "Stocks",
        "USER": "postgres",
        "PASSWORD": "password",
        "HOST": "127.0.0.1",
        "PORT": "5432",
        "encoding": "utf-8",

    }
}

2

Answers


  1. I have the strong feeling, that your psycopg2 package is too old. Maybe some old version got installed as a dependency for another package. Update it with: pip install --upgrade psycopg2.

    The dictionary does not work with an "encoding" keyword. If you want to specify the encoding do it like this:

    DATABASES = {
        "default": {
            "ENGINE": "django.db.backends.postgresql",
            "NAME": "Stocks",
            "USER": "postgres",
            "PASSWORD": "password",
            "HOST": "127.0.0.1",
            "PORT": "5432",
            "OPTIONS": {
                "options": "-c client_encoding=utf8"
            }
        }
    }
    
    Login or Signup to reply.
  2. Most likely, this is an issue with Psycopg; it causes a lot of problems, especially with some 2.x releases.  Try to rollback to 2.9.9 if you have a requirements.txt, you can look for the package in there and change the version or, as suggested above by @Tarquinius, you can install it via terminal.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search