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

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"

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


当前回答

在终端试试:

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

其他回答

在终端试试:

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

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

在我的例子中,我使用了不同的端口。默认为5432。我用的是5433。这招对我很管用:

$ psql -f update_table.sql -d db_name -U db_user_name -h 127.0.0.1 -p 5433

最简单的解决方法:

CREATE USER dev WITH PASSWORD 'dev';
CREATE DATABASE test_development;
GRANT ALL PRIVILEGES ON DATABASE test_development to dev;
ALTER ROLE dev CREATEROLE CREATEDB;

对等身份验证意味着postgres向操作系统询问您的登录名并使用此名进行身份验证。在postgres上使用对等身份验证以“dev”用户登录时,必须同时是操作系统上的“dev”用户。

您可以在Postgresql文档中找到身份验证方法的详细信息。

提示:如果没有任何身份验证方法,请断开服务器与网络的连接,并为“localhost”使用方法“trust”(并在启用方法“trust”时再次检查您的服务器是否无法通过网络访问)。