I simply stopped using psycopg2 and switched to psycopg (aka psycopg3) and everything worked perfectly. I spent a whole day trying to understand why it kept giving this error, and I came to no conclusion. I tried thousands of things and nothing worked, so I just switched.