pythonpostgresqlflaskutf-8psycopg2

UnicodeDecodeError When Connecting to PostgreSQL Using psycopg2 in Python


I am encountering an issue when trying to connect to PostgreSQL using the psycopg2 library in Python. I get the following error:

Traceback (most recent call last):
  File "c:\Users\ferre\Projects\PycharmProjects\Portal-de-Noticias\backend\user_registration\app.py", line 3, in <module>
    from noticias import noticias_bp
  File "c:\Users\ferre\Projects\PycharmProjects\Portal-de-Noticias\backend\user_registration\noticias.py", line 8, in <module>
    conn = psycopg2.connect(
           ^^^^^^^^^^^^^^^^^
  File "C:\Users\ferre\Projects\PycharmProjects\Portal-de-Noticias\.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 0xe7 in position 78: invalid continuation byte

Here is a snippet of my code where I attempt to make the connection:

import psycopg2
from flask import Blueprint, jsonify, request

# Creating the Blueprint
noticias_bp = Blueprint('noticias', __name__)

# Database connection
conn = psycopg2.connect(
    dbname="portalDeNoticias",
    user="postgres",
    password="mk785",
    host="localhost",
    port='5432'
)
cur = conn.cursor()

Additionally, here is a similar function used elsewhere in the code to connect to the database:

def connect_db():
    conn = psycopg2.connect(
        dbname="portalDeNoticias",
        user="postgres",
        password="mk785",
        host="localhost",
        port='5432'
    )
    return conn

It seems the error is related to a UTF-8 decoding issue, but I'm not sure what exactly is causing this or how to fix it.

Could someone help me understand what might be wrong and how to resolve this error?

Thank you!


Solution

  • I think this may comes from connection string. please delete them and re-enter again using ASCII. make sure to search your code base to replace all.

    translation for Notiçias