为什么我在尝试使用 psycopg2 进行连接时收到对等身份验证失败错误?



所以我在 Ubuntu 上成功安装了 Postgres,并尝试进行一些基本连接并使用默认用户名以外的另一个用户名在数据库中创建一个表(postgres(并且出现不足。从我收集到的信息来看,我认为这可能与权限有关?我想要的是能够使用 postgres 以外的一些超级用户来创建表和做一些事情。

"psql 示例 3"和"\l"表示示例 3 数据库已成功创建。我现在有一个数据库列表,包括默认的 postgres、template0、template1 和 example3,所有这些都以所有者作为 postgres。然后我遇到的问题是运行 demoscript.py 给出致命的"用户'thisuser'的对等身份验证失败">

#Create the db and user with superuser permissions
sudo -u postgres -i
createdb example3
createuser --interactive --pwprompt
#role:thisuser
#pwd:thispass
#superuser?:Y
#demoscript.py
import psycopg2 
connection = psycopg2.connect('dbname=example3 user=thisuser password=thispass')
cursor = connection.cursor()
cursor.execute('DROP TABLE IF EXISTS todos;')
cursor.execute('''
CREATE TABLE todos(
id serial PRIMARY KEY,
description VARCHAR NOT NULL
);
''')
connection.commit()
cursor.close()
connection.close()

预期结果是,在 example3 db 中查找待办事项表后,应显示为已创建。但我只是得到致命的错误。

如果未在连接中指定主机,它将尝试通过 Unix 套接字进行连接。默认情况下,PostgreSQL设置为在这些用户上使用对等身份验证,这意味着它将PostgreSQL用户名与当前登录的操作系统用户进行比较。如果将连接更改为:

connection = psycopg2.connect('dbname=example3 user=thisuser password=thispass host=localhost')

这应该导致它使用 TCP/IP 连接的身份验证设置,在我使用的大多数系统上默认为密码身份验证。

最新更新