admin管理员组

文章数量:1399947

I'm working on a personal project and using Docker and psycopg2 for the first time. I deployed a PostgreSQL server via Docker and am trying to connect to it using a Python script (with the goal of using Airflow DAGs in the future).

However, no matter what I try, I always get the same error when attempting to connect to the database:

Python Code is very simple :

import psycopg2

conn = psycopg2.connect(
    dbname="testdb", user="user", password="pass", host="localhost", port=5432
)

conn.close()

Error Message :

conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 103: invalid continuation byte

I have checked all parameters to ensure there are no non-UTF-8 characters, and PostgreSQL is correctly set to UTF-8.

What I have already tried without success:

  • Checking Python's encoding -> UTF-8
  • Adding client_encoding='utf8' to the connection
  • Verifying that PostgreSQL encoding -> UTF8
  • Checking Windows encoding settings

Despite all these attempts, the issue persists. Does anyone have an idea how to fix this? Thanks in advance for your help!

本文标签: pythonPsycopg2 to PostgresSQL DockerUTF8 Connection errorStack Overflow