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

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"

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


当前回答

pg_dump -h localhost -U postgres -F c -b -v -F mydb。备份mydb

其他回答

我只需要添加-h localhost

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

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

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

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

然后在命令行上

$> source ./.bashrc

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

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

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

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

sudo -u username psql