用户在Postgres 9.5中具有所有权限,但对等身份验证失败。

我想创建一个只能访问指定数据库的用户。然而,该用户应具有所有权限。我在Ubuntu 14.04上使用Postgresql 9.5。首先,我创建一个新用户:
$createuser --interactive joe
  Shall the new role be a superuser? (y/n) n
  Shall the new role be allowed to create databases? (y/n) n
  Shall the new role be allowed to create more new roles? (y/n) n

接下来,我将使用所有者为Joe创建一个新的数据库:
 $sudo -u postgres psql 
 $CREATE DATABASE myDB OWNER joe;
 $GRANT ALL ON DATABASE myDB TO joe;

接下来,我尝试连接用户Joe以连接到我的数据库myDB:

$psql myDB -U joe
psql: FATAL:  Peer authentication failed for user "joe" 

下一步我该做什么?

4这不是离题的。因为在Ubuntu上安装软件是属于主题范围内的。 - Anwar
你的问题解决了吗? - Anwar
1个回答

  1. 以 root 权限打开 /etc/postgresql/9.5/main/pg_hba.conf

    sudo nano /etc/postgresql/9.5/main/pg_hba.conf
    
  2. 在这些行中将 peer 更改为 md5

    更改前

    # "local" is for Unix domain socket connections only
    local   all             all                                     peer
    # IPv4 local connections:
    host    all             all             127.0.0.1/32            peer
    # IPv6 local connections:
    host    all             all             ::1/128                 peer
    

    更改后

    # "local" is for Unix domain socket connections only
    local   all             all                                     md5
    # IPv4 local connections:
    host    all             all             127.0.0.1/32            md5
    # IPv6 local connections:
    host    all             all             ::1/128                 md5
    
  3. 按下 Ctrl-O 保存文件。使用 Ctrl-X 退出 nano

  4. 使用以下命令重启 postgresql

    sudo service postgresql restart
    

1谢谢,我不得不将本地对等线路更改为MD5,在此之后我成功登录了。 - Qeychon
2更新任何版本:要检查配置文件,请使用以下命令:sudo -u postgres psql -c "SHOW config_file" - Peter Krauss
我的显示为ident而不是peer,host all all 127.0.0.1/32 identhost all all ::1/128 ident - Joseph K.