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
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: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.