Skip to content

[Windows] Error message from pgsql server is not decoded correctly when the PGconn status is ConnStatus.BAD #194

@lyq1996

Description

@lyq1996

First of all, sorry for my poor english..

I am writing a library for pgsql to crack the password of a database user from a password hash, and I use psycopg3 to verify that the password is correct. I found that when the connection is not established and the encoding is not utf-8, psycopg3 does not seem to correctly decode the error message from the server.

My pgsql server runs on windows 10, and the server version is 13.3 and encoding GBK(CP936) for messages displayed.
postgresql.conf:

# These settings are initialized by initdb, but they can be changed.
lc_messages = 'Chinese (Simplified)_China.936'			# locale for system error message
					# strings
lc_monetary = 'Chinese (Simplified)_China.936'			# locale for monetary formatting
lc_numeric = 'Chinese (Simplified)_China.936'			# locale for number formatting
lc_time = 'Chinese (Simplified)_China.936'				# locale for time formatting

For example, If the user's password was wrong or datebase for login doesn't existed, the obj.status will be ConnStatus.BAD(https://github.com/psycopg/psycopg/blob/master/psycopg/psycopg/pq/misc.py#L64), and then the msg will decoded as utf-8 by default and cause an error, then the part of msg which can not be decoded will be replaced by '\ufffd'.

Here is the exmaple code:

# coding=utf-8
import time
import psycopg

try:
    conn = psycopg.connect("user=user1 password='123456' host='127.0.0.1' port='5432' client_encoding='GBK'")
except psycopg.Error as ex:
    print(str(ex))

The error message should be "connection failed: 致命错误: 数据库 "user1" 不存在" which meaning "FATAL: database "user1" does not exist\n".

screenshot

When the connection established, the error message can be decoded correctly according to the given client_encoding:

# coding=utf-8
import time
import psycopg

try:
    conn = psycopg.connect("user=postgres password='123456' host='127.0.0.1' port='5432' client_encoding='GBK'")
    conn.execute("""
                select * from qwertsdnmsdjffks;)
                """)
    conn.commit()
except psycopg.Error as ex:
    print(str(ex))

screenshot2

Hope you can confirm and fix, thanks a lot.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions