为什么在尝试使用 psycopg2 连接时出现对等身份验证失败错误?
Why am I getting peer authentication failed error when trying to connect using psycopg2?
所以我在 Ubuntu 上成功安装了 Postgres,我正在尝试进行一些基本连接并使用默认用户名 (postgres) 以外的其他用户名在数据库中创建 table 并即将推出简而言之。据我所知,我认为这可能与权限有关?我想要的是能够使用 postgres 以外的一些超级用户来创建 tables 并做一些事情。
"psql example3" 和 "\l" 表示 example3 数据库创建成功。我现在有一个数据库列表,包括默认的 postgres、template0、template1 和 example3,所有这些数据库的所有者都是 postgres。我 运行 遇到的问题是 运行ning demoscript.py 给出了致命的 "peer authentication failed for user '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()
预期结果是待办事项 table 在 example3 数据库中查找后应显示为已创建。但我刚得到致命错误。
当您未在连接中指定主机时,它会尝试通过 Unix 套接字进行连接。默认情况下,PostgreSQL 设置为对这些用户使用对等身份验证,这意味着它将 PostgreSQL 用户名与当前登录的 OS 用户进行比较。如果您将连接更改为:
connection = psycopg2.connect('dbname=example3 user=thisuser password=thispass host=localhost')
这应该会导致它使用 TCP/IP 连接的身份验证设置,在我使用的大多数系统上默认使用密码身份验证。
所以我在 Ubuntu 上成功安装了 Postgres,我正在尝试进行一些基本连接并使用默认用户名 (postgres) 以外的其他用户名在数据库中创建 table 并即将推出简而言之。据我所知,我认为这可能与权限有关?我想要的是能够使用 postgres 以外的一些超级用户来创建 tables 并做一些事情。
"psql example3" 和 "\l" 表示 example3 数据库创建成功。我现在有一个数据库列表,包括默认的 postgres、template0、template1 和 example3,所有这些数据库的所有者都是 postgres。我 运行 遇到的问题是 运行ning demoscript.py 给出了致命的 "peer authentication failed for user '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()
预期结果是待办事项 table 在 example3 数据库中查找后应显示为已创建。但我刚得到致命错误。
当您未在连接中指定主机时,它会尝试通过 Unix 套接字进行连接。默认情况下,PostgreSQL 设置为对这些用户使用对等身份验证,这意味着它将 PostgreSQL 用户名与当前登录的 OS 用户进行比较。如果您将连接更改为:
connection = psycopg2.connect('dbname=example3 user=thisuser password=thispass host=localhost')
这应该会导致它使用 TCP/IP 连接的身份验证设置,在我使用的大多数系统上默认使用密码身份验证。