用户postgres的Psycopg2对等身份验证

时间:2021-03-13 23:41:17

I seem to have correctly installed PostgreSQL 9.5.5. and Psycopg2 on Ubuntu 16.04, and can log in via:

我似乎已经正确安装了PostgreSQL 9.5.5。和Usntu 16.04上的Psycopg2,可以通过以下方式登录:

sudo -u postgres psql

If I then issue \conninfo, I get the following:

如果我然后发出\ conninfo,我得到以下内容:

You are connected to database "postgres" as user "postgres" via socket in "/var/run/postgresql" at port "5432".

Surely I should be able to connect via psycopg2 in the same fashion as shown here, but the script:

当然我应该能够通过psycopg2以与此处所示相同的方式连接,但脚本:

#!/usr/bin/python
import psycopg2
conn = psycopg2.connect("dbname=postgres user=postgres") 
conn.close()

gives me:

psycopg2.OperationalError: FATAL:  Peer authentication failed for user "postgres"

I only want PostgreSQL for personal usage, so I don't want to enable TCP authentication.

我只希望PostgreSQL用于个人用途,因此我不想启用TCP身份验证。

How do I correctly use peer authentication with user "postgres" in Psycopg2?

如何在Psycopg2中使用用户“postgres”正确使用对等身份验证?

3 个解决方案

#1


9  

Peer authentication works by comparing the Postgres username in your connection string to the name of the Linux user who is running the script.

对等身份验证的工作原理是将连接字符串中的Postgres用户名与运行脚本的Linux用户的名称进行比较。

Try running your Python script with sudo -u postgres.

尝试使用sudo -u postgres运行Python脚本。

#2


4  

You need to supply the host

您需要提供主机

conn = psycopg2.connect("dbname='template1' user='dbuser' host='localhost' password='dbpass'")

#3


1  

This is sort of how yoru call should look like.

这就是yoru调用的样子。

!/usr/bin/python
import psycopg2
conn = psycopg2.connect(database="postgres", user="postgres", password="postgres", port=5432)

conn.close()

#1


9  

Peer authentication works by comparing the Postgres username in your connection string to the name of the Linux user who is running the script.

对等身份验证的工作原理是将连接字符串中的Postgres用户名与运行脚本的Linux用户的名称进行比较。

Try running your Python script with sudo -u postgres.

尝试使用sudo -u postgres运行Python脚本。

#2


4  

You need to supply the host

您需要提供主机

conn = psycopg2.connect("dbname='template1' user='dbuser' host='localhost' password='dbpass'")

#3


1  

This is sort of how yoru call should look like.

这就是yoru调用的样子。

!/usr/bin/python
import psycopg2
conn = psycopg2.connect(database="postgres", user="postgres", password="postgres", port=5432)

conn.close()