获取错误:当试图让pgsql使用rails时,用户“postgres”的对等身份验证失败

本文翻译自:Getting error: Peer authentication failed for user “postgres”, when trying to get pgsql working with rails

I'm getting the error: 我收到错误:

FATAL: Peer authentication failed for user "postgres"

when I try to make postgres work with Rails. 当我尝试使用Rails进行postgres时。

Here's my pg_hba.conf , my database.yml , and a dump of the full trace . 这是我的pg_hba.conf ,我的database.yml ,以及完整跟踪转储

I changed authentication to md5 in pg_hba and tried different things, but none seem to work. 我在pg_hba中将身份验证更改为md5并尝试了不同的操作,但似乎都没有。

I also tried creating a new user and database as per Rails 3.2, FATAL: Peer authentication failed for user (PG::Error) 我还尝试按照Rails 3.2创建一个新的用户和数据库,致命:用户的对等身份验证失败(PG ::错误)

But they don't show up on pgadmin or even when I run sudo -u postgres psql -l . 但是他们没有出现在pgadmin上,甚至当我运行sudo -u postgres psql -l

Any idea where I'm going wrong? 知道我哪里错了吗?


#1楼

参考:https://stackoom.com/question/1GJNS/获取错误-当试图让pgsql使用rails时-用户-postgres-的对等身份验证失败


#2楼

The problem is still your pg_hba.conf file ( /etc/postgresql/9.1/main/pg_hba.conf* ). 问题仍然是你的pg_hba.conf文件( /etc/postgresql/9.1/main/pg_hba.conf* )。

This line: 这一行:

local   all             postgres                                peer

Should be: 应该:

local   all             postgres                                md5

* If you can't find this file, running locate pg_hba.conf should show you where the file is. *如果找不到此文件,运行locate pg_hba.conf应该会显示该文件的位置。

After altering this file, don't forget to restart your PostgreSQL server. 更改此文件后,请不要忘记重新启动PostgreSQL服务器。 If you're on Linux, that would be sudo service postgresql restart . 如果你在Linux上,那将是sudo service postgresql restart

These are brief descriptions of both options according to the official PostgreSQL docs on authentication methods . 这些是根据官方PostgreSQL文档关于身份验证方法的两个选项的简要描述。

Peer authentication 对等身份验证

The peer authentication method works by obtaining the client's operating system user name from the kernel and using it as the allowed database user name (with optional user name mapping). 对等身份验证方法的工作原理是从内核获取客户端的操作系统用户名,并将其用作允许的数据库用户名(使用可选的用户名映射)。 This method is only supported on local connections. 仅在本地连接上支持此方法。

Password authentication 密码验证

The password-based authentication methods are md5 and password. 基于密码的身份验证方法是md5和密码。 These methods operate similarly except for the way that the password is sent across the connection, namely MD5-hashed and clear-text respectively. 除了通过连接发送密码的方式外,这些方法的操作类似,分别是MD5散列和明文。

If you are at all concerned about password "sniffing" attacks then md5 is preferred. 如果你完全担心密码“嗅探”攻击,那么首选md5。 Plain password should always be avoided if possible. 如果可能,应始终避免使用普通密码。 However, md5 cannot be used with the db_user_namespace feature. 但是,md5不能与db_user_namespace功能一起使用。 If the connection is protected by SSL encryption then password can be used safely (though SSL certificate authentication might be a better choice if one is depending on using SSL). 如果连接受SSL加密保护,则可以安全地使用密码(但如果依赖于使用SSL,则SSL证书身份验证可能是更好的选择)。

Sample location for pg_hba.conf : pg_hba.conf示例位置:
/etc/postgresql/9.1/main/pg_hba.conf


#3楼

I had the same problem. 我有同样的问题。

The solution from depa is absolutely correct. 来自depa的解决方案绝对正确。

Just make sure that u have a user configured to use PostgreSQL. 只需确保您已将用户配置为使用PostgreSQL。

Check the file: 检查文件:

$ ls /etc/postgresql/9.1/main/pg_hba.conf -l

The permission of this file should be given to the user you have registered your psql with. 应该将此文件的权限授予您已注册psql的用户。

Further. 进一步。 If you are good till now.. 如果你现在好,..

Update as per @depa's instructions. 根据@depa的说明更新。

ie

$ sudo nano /etc/postgresql/9.1/main/pg_hba.conf

and then make changes. 然后进行更改。


#4楼

After installing Postgresql I did the below steps. 安装Postgresql后,我做了以下步骤。

  1. open the file pg_hba.conf for Ubuntu it will be in /etc/postgresql/9.x/main and change this line: 打开Ubuntu的文件pg_hba.conf ,它将在/etc/postgresql/9.x/main并更改此行:
local   all             postgres                                peer

to

local   all             postgres                                trust
  1. Restart the server 重启服务器
 sudo service postgresql restart 
  1. Login into psql and set your password 登录psql并设置密码

psql -U postgres psql -U postgres

ALTER USER postgres with password 'your-pass';
  1. Finally change the pg_hba.conf from 最后更改pg_hba.conf
local   all             postgres                                trust

to

local   all             postgres                                md5

After restarting the postgresql server, you can access it with your own password 重新启动postgresql服务器后,您可以使用自己的密码访问它

Authentication methods details: 验证方法细节:

trust - anyone who can connect to the server is authorized to access the database 信任 - 任何可以连接到服务器的人都有权访问数据库

peer - use client's operating system user name as database user name to access it. peer - 使用客户端的操作系统用户名作为数据库用户名来访问它。

md5 - password-base authentication md5 - 基于密码的身份验证

for further reference check here 有关进一步参考, 请点击此


#5楼

If you connect over localhost (127.0.0.1) you shouldn't experience that particular issue. 如果您通过localhost(127.0.0.1)连接,则不应遇到该特定问题。 I wouldn't muck much with the pg_hba.conf but instead I would adjust your connection string: 我不会吝啬pg_hba.conf但我会调整你的连接字符串:

psql -U someuser -h 127.0.0.1 database

where someuser is your user you're connecting as and database is the database your user has permission to connect to. 其中某个用户是您正在连接的用户,而数据库是您的用户有权连接的数据库。

Here is what I do on Debian to setup postgres: 这是我在Debian上设置postgres的方法:

http://www.postgresql.org/download/linux/debian/  (Wheezy 7.x)

as root …

    root@www0:~# echo "deb http://apt.postgresql.org/pub/repos/apt/ wheezy-pgdg main" >> /etc/apt/sources.list

    root@www0:~# wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -

    root@www0:~# apt-get update

    root@www0:~# apt-get install postgresql-9.4        

    root@www0:~# su - postgres 

    postgres@www0:~$ createuser --interactive -P someuser
    Enter password for new role:
    Enter it again:
    Shall the new role be a superuser? (y/n) n
    Shall the new role be allowed to create databases? (y/n) y
    Shall the new role be allowed to create more new roles? (y/n) n

    postgres@www0:~$ createdb -O someuser database

    postgres@www0:~$ psql -U someuser -h 127.0.0.1 database

Enjoy! 请享用!


#6楼

以下命令对我有用:

psql -d myDb -U username -W
发布了0 篇原创文章 · 获赞 8 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/asdfgh0077/article/details/105556373
今日推荐