PostgreSQL 登录报错 psql: FATAL: Peer authentication failed for user

在 peer 错误前,我安装 pgsql 后使用默认用户 postgres 登录的时候报错,当我直接敲命令 # psql

psql: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

顺便也一起讲下!

这是因为pgsql服务器没有运行,我只安装了 postgresql 包,没有安装 postgresql-server ,装上之后运行服务 $ sudo /etc/init.d/postgresql start

好了开始讲正题,运行是解决了,用户和数据库也创建好了,但是创建以及登录期间又会碰到一个常见的报错。

我尝试登录用户连接其数据库 ,

postgres@linux-tvqg:/home/hang> psql -U aann -d aann_db 
    psql: FATAL:  Peer authentication failed for user "aann"

解决方法很简单,使用和pgsql用户相同的系统用户登录,
> sudo -u aann psql -U aann -d aann_db
先来讲讲出现此错误的原因。仔细看报错信息,其中peer authentication 比较注目,很明显,错误和权限有关联。

stackoverflow上有相关答案,原址: psql: FATAL: Peer authentication failed for user “dev”

psql的连接建立于Unix Socket上默认使用peer authentication,所以必须要用和 数据库用户 相同的 系统用户 进行登录。

还有一种方法,将peer authentiction 改为 md5,并给数据库设置密码。修改配置文件,
/etc/postgresql/x.x/main/pg_hba.conf

local   all          postgres         peer
// change to
local   all          postgres          md5

重新启动pgsql,
/etc/init.d/postgresql restart

关于 peer authentication 和 md5 有什么不同,有关它们的详细内容以后再做了解。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容