psycopg / psycopg2

PostgreSQL database adapter for the Python programming language
https://www.psycopg.org/
Other
3.36k stars 504 forks source link

Encoding question: SystemError: 'cp1252', Python 3.5, psycopg2 '2.6.1 (dt dec pq3 ext lo64)' #442

Open apiszcz opened 8 years ago

apiszcz commented 8 years ago

Is there an explicit encoding requirement that I need to set? Thanks

cur.execute("""CREATE DATABASE IF NOT EXISTS TEST""") *\ SystemError: decoding with 'cp1252' codec failed (SystemError: returned a result with an error set)

dvarrazzo commented 8 years ago

In what language is this postgres installation?

apiszcz commented 8 years ago

English on Windows 10 i switched to PostgreSQL no issues.​

On Thu, Jul 7, 2016 at 7:19 AM, Daniele Varrazzo notifications@github.com wrote:

In what language is this postgres installation?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/psycopg/psycopg2/issues/442#issuecomment-231050826, or mute the thread https://github.com/notifications/unsubscribe/ABXVTSRMgKe2pxXExI1A9Pp7j08cl8jdks5qTOCngaJpZM4I5CRM .

dvarrazzo commented 8 years ago

The above causes an error because "create database if not exists" doesn't exist.

In [11]: cnn.set_client_encoding('win1252')

In [12]: cur.execute("create database if not exists asdf ")
---------------------------------------------------------------------------
ProgrammingError                          Traceback (most recent call last)
<ipython-input-12-8557d908ff45> in <module>()
----> 1 cur.execute("create database if not exists asdf ")

ProgrammingError: syntax error at or near "not"
LINE 1: create database if not exists asdf 
                           ^

I believe this message gets localized in a locale that has chars not supported in cp1252, which postgres emits anyway but python then barfs trying to decode, in a codepath that doesn't handle the exception properly. I should make myself a Japanese database to play with...

However if you say that postgres is in English for you, then my theory doesn't work here because, as you see above there is no problem receiving the exception in English.

jleclanche commented 6 years ago

@dvarrazzo I think it's the other way around, python is in cp1252, postgres is in utf8.