postgresql 用户 postgres 的 Psycopg2 对等身份验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40537159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:26:31  来源:igfitidea点击:

Psycopg2 peer authentication for user postgres

pythonpostgresqlpsycopg2ubuntu-16.04

提问by Yuri Lifanov

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。和 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”的对等身份验证?

回答by Nick Barnes

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.

回答by moohaad

You need to supply the host

您需要提供主机

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

回答by Illusionist

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()

回答by Afjal Ahamad

these are the some authentication method of postgresql

这些是 postgresql 的一些身份验证方法

peermeans it will trust the identity (authenticity) of UNIX user. So not asking for a password.

peer意味着它将信任 UNIX 用户的身份(真实性)。所以不要求密码。

md5means it will always ask for a password, and validate it after hashing with MD5.

md5意味着它总是要求输入密码,并在用 MD5 散列后验证它。

trustmeans 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