简体   繁体   English

用户 postgres 的 Psycopg2 对等身份验证

[英]Psycopg2 peer authentication for user postgres

I seem to have correctly installed PostgreSQL 9.5.5.我似乎已经正确安装了 PostgreSQL 9.5.5。 and Psycopg2 on Ubuntu 16.04, and can log in via:和 Ubuntu 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”的对等身份验证?

您需要提供主机

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

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 脚本。

This is sort of how yoru call should look like.这就是 yoru call 的样子。

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

conn.close()

these are the some authentication method of postgresql这些是 postgresql 的一些身份验证方法

peer means it will trust the identity (authenticity) of UNIX user. peer意味着它将信任 UNIX 用户的身份(真实性)。 So not asking for a password.所以不要求密码。

md5 means it will always ask for a password, and validate it after hashing with MD5. md5意味着它总是要求输入密码,并在用 MD5 散列后验证它。

trust means it will never ask for a password, and always trust any connection.信任意味着它永远不会要求密码,并且始终信任任何连接。

I your case youe have to cahnge like this:我你的情况你必须像这样改变:

host all all 127.0.0.1/32 ident托管所有所有 127.0.0.1/32 标识

to

host all all 127.0.0.1/32 md5托管所有所有 127.0.0.1/32 md5

and

host all all ::1/128 ident主持所有所有:: 1/128 ident

to

host all all ::1/128 md5主持所有所有:: 1/128 md5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM