N
Neon6mo ago
extended-salmon

'utf-8' codec can't decode byte 0xc2 in position 67: invalid continuation byte

guys I did everything step-by-step on https://neon.tech/postgresql/postgresql-python/connect but this error keeps showing up, what's wrong?
Neon
PostgreSQL Python: Connecting to PostgreSQL Server
In this tutorial, you will learn how to connect to the PostgreSQL database server from Python using the psycopg2 package.
1 Reply
like-gold
like-gold5mo ago
If you see a 'utf-8' codec can't decode byte error when running connect.py, check that your database.ini file is saved using UTF-8 encoding. Some editors may default to a different encoding (like Windows-1252). Open the file in a code editor and save it as UTF-8. You can also update your config.py file to explicitly read with UTF-8 encoding. 1. Check encoding of database.ini The ConfigParser in config.py assumes that the .ini file is UTF-8 encoded by default. If the file was created or edited in an editor that saved it with a different encoding (e.g., Windows-1252), it could trigger this error. Fix: Re-save the file in UTF-8 format: Open the database.ini file in a code editor like VS Code or Notepad++. Save it with UTF-8 encoding: In VS Code: File > Save with Encoding > UTF-8 In Notepad++: Encoding > Convert to UTF-8 → Save. 2. Check for hidden characters There might be an invisible or non-printable character in database.ini. Common causes: Copy-pasting from a website or document Adding special characters (e.g., smart quotes or non-breaking spaces) Fix: Delete and retype the line that causes the error. Or recreate the file manually with clean text. ✍️ 3. Explicitly set the encoding when reading the file You can update config.py to read with explicit UTF-8 encoding:
from configparser import ConfigParser

def load_config(filename='database.ini', section='postgresql'):
parser = ConfigParser()
with open(filename, encoding='utf-8') as f:
parser.read_file(f)
config = {}
if parser.has_section(section):
params = parser.items(section)
for param in params:
config[param[0]] = param[1]
else:
raise Exception(f'Section {section} not found in the {filename} file')
return config
This avoids any encoding assumption mismatch.
from configparser import ConfigParser

def load_config(filename='database.ini', section='postgresql'):
parser = ConfigParser()
with open(filename, encoding='utf-8') as f:
parser.read_file(f)
config = {}
if parser.has_section(section):
params = parser.items(section)
for param in params:
config[param[0]] = param[1]
else:
raise Exception(f'Section {section} not found in the {filename} file')
return config
This avoids any encoding assumption mismatch.

Did you find this page helpful?