当我创建一个新用户,但它不能登录数据库。 我是这样做的:

postgres@Aspire:/home/XXX$ createuser dev
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) y

然后创建一个数据库:

postgres@Aspire:/home/XXX$ createdb -O dev test_development

之后,我尝试psql -U dev -W test_development登录,但得到错误:

psql: FATAL:  Peer authentication failed for user "dev"

我试图解决这个问题,但失败了。


当前回答

当我遇到这种情况时,这对我来说很管用:

sudo -u username psql

其他回答

我只需要添加-h localhost

为了方便以后看到这个,postgres在我的Ubuntu服务器的/usr/lib/postgresql/10/bin目录下。

我将它添加到.bashrc文件的PATH中,并在末尾添加这一行

PATH=$PATH:/usr/lib/postgresql/10/bin

然后在命令行上

$> source ./.bashrc

我刷新了bash环境。现在我可以在任何目录中使用postgres -D /

Try:

psql -U user_name  -h 127.0.0.1 -d db_name

在哪里

-U为数据库用户名 -h是本地服务器的主机名/IP,从而避免Unix域套接字 -d是要连接的数据库名称

然后,Postgresql将其计算为“网络”连接,而不是Unix域套接字连接,因此不像你在pg_hba.conf中看到的那样,将其计算为“本地”连接:

local   all             all                                     peer

虽然@flaviodesousa的答案可以工作,但它也强制所有用户(其他所有人)输入密码。

有时对其他人保持对等身份验证是有意义的,但对服务用户例外。在这种情况下,你会想要在pg_hba.conf中添加一行,看起来像这样:

local   all             some_batch_user                         md5

我建议你在注释标题行下面添加这一行:

# TYPE  DATABASE        USER            ADDRESS                 METHOD
local   all             some_batch_user                         md5

重启PostgreSQL需要使用

sudo service postgresql restart

如果你使用的是9.3,你的pg_hba.conf很可能是:

/etc/postgresql/9.3/main/pg_hba.conf

在终端试试:

>> psql -U role_name -d database -h hostname.<domain>.com -W