密码验证失败,输入复杂密码 [英] Password authentication fails with complex password
问题描述
我编写了一个Python脚本,该脚本使用 psycopg2 2.6 和 Python 2.7.8 连接到本地PostgreSQL数据库.连接设置和命令如下:
I wrote a Python script which connects to the local PostgreSQL database using psycopg2 2.6 and Python 2.7.8. The connection settings and commands are the following:
HOST = '127.0.0.1'
DATABASE_NAME = 'myappdatabase'
DATABASE_PORT = '5432'
DATABASE_USER = 'myappuser'
DATABASE_PASSWORD = 'secret'
DATABASE_TABLE = 'myappdata'
def _database_connection():
conn_string = "host='{0}' dbname='{1}' port='{2}' user='{3}' \
password='{4}'".format(HOST, DATABASE_NAME, DATABASE_PORT, \
DATABASE_USER, DATABASE_PASSWORD)
return psycopg2.connect(conn_string)
该脚本可以在没有安装PostgreSQL 9.4的计算机上正常运行. sudo vi /etc/postgresql/9.4/main/pg_hba.conf
中的配置未修改,看起来像这样,没有注释:
The script works without problems on machine one which has PostgreSQL 9.4 installed. The configuration in sudo vi /etc/postgresql/9.4/main/pg_hba.conf
is not modified and looks like this, without comments:
local all postgres peer
local all all peer
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
我还可以使用 pgAdmin III 成功连接到相同的数据库.
I can also successfully connect to the same database using pgAdmin III.
在具有相同设置的第二台计算机上,我无法通过脚本连接.发生以下错误:
On a second machine with the same setup I cannot connect via the script. The following error occurs:
psycopg2.OperationalError:严重:用户"myappuser"的密码身份验证失败
致命:用户"myappuser"的密码身份验证失败
psycopg2.OperationalError: FATAL: password authentication failed for user "myappuser"
FATAL: password authentication failed for user "myappuser"
这两台机器之间唯一的差异是数据库密码.因此,我将其更改为简单的密码-繁荣起来,它可以正常工作.因此,我将其改回了一个复杂的...,它不再再次起作用.密码是这样的:
The only difference between these two machines is the database password. So I changed it to a simple password - and boom, it works. So I changed it back to a complicated ... and it does not work again. The password is something like this:
DATABASE_PASSWORD ='zyx @ 12AA \ w2'
DATABASE_PASSWORD = 'zyx@12AA\w2'
所以我想:这很愚蠢.一定是我的错误." 因此,我将第一台机器上的数据库密码设置为与第二台机器上的相同.而且,Python脚本也会失败.
So I thought: "This is stupid. It must be my mistake." So I set the database password on machine one to the same as on the second machine. And whoop the Python script also fails.
推荐答案
在这种情况下,密码中的反斜杠被解释为转义符. Python会将\w
中的反斜杠视为文字\
,即使未将其指定为原始字符串,因为\w
不是有效的转义序列,但基础库也
The backslash in the password in this case is interpreted as escape character. Python would treat the backslash in \w
as literal \
even if it's not specified as a raw string because \w
is not a valid escape sequence, but the underlying library also parses escape sequences, therefore the backslash has to be escaped (doubled).
为避免这种情况,请指定连接参数作为关键字参数 :
To avoid this, specify the connection parameters as keyword arguments instead:
psycopg2.connect(host=HOST,
database=DATABASE_NAME,
port=DATABASE_PORT,
user=DATABASE_USER,
password=DATABASE_PASSWORD)
这可以避免密码中的特殊字符出现问题.另外,密码中的单引号字符会破坏连接字符串.
This avoids problems with special characters in passwords. Also a single quote chacacter in the password would break the connection string.
这篇关于密码验证失败,输入复杂密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!