Postgresql:用户“postgres”的密码认证失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7695962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Postgresql: password authentication failed for user "postgres"
提问by I159
I have installed PostgreSQL 8.4, Postgres client and Pgadmin 3. Authentication failed for user "postgres" for both console client and Pgadmin. I have typed user as "postgres" and password "postgres", because it worked before. But now authentication is failed. I did it before a couple of times without this problem. What should I do? And what happens?
我已经安装了 PostgreSQL 8.4、Postgres 客户端和 Pgadmin 3。控制台客户端和 Pgadmin 的用户“postgres”的身份验证失败。我输入了用户“postgres”和密码“postgres”,因为它以前工作过。但是现在认证失败了。我之前做过几次没有这个问题。我该怎么办?会发生什么?
psql -U postgres -h localhost -W
Password for user postgres:
psql: FATAL: password authentication failed for user "postgres"
FATAL: password authentication failed for user "postgres"
回答by A.H.
If I remember correctly the user postgres
has no DBpassword set on Ubuntu by default. That means, that you can login to that account only by using the postgres
OS useraccount.
如果我没记错的话,默认情况下用户postgres
没有在 Ubuntu 上设置数据库密码。这意味着,您只能使用postgres
操作系统用户帐户登录到该帐户。
Assuming, that you have root
access on the box you can do:
假设您可以root
访问您可以执行的框:
sudo -u postgres psql
If that fails with a database "postgres" does not exists
error, then you are most likely not on a Ubuntu or Debian server :-) In this case simply add template1
to the command:
如果失败并出现database "postgres" does not exists
错误,那么您很可能不在 Ubuntu 或 Debian 服务器上:-) 在这种情况下,只需添加template1
到命令中:
sudo -u postgres psql template1
If any of those commands fail with an error psql: FATAL: password authentication failed for user "postgres"
then check the file /etc/postgresql/8.4/main/pg_hba.conf
: There must be a line like this as the first non-comment line:
如果这些命令中的任何一个失败并出现错误,psql: FATAL: password authentication failed for user "postgres"
请检查文件/etc/postgresql/8.4/main/pg_hba.conf
:必须有这样一行作为第一个非注释行:
local all postgres ident
For newer versions of PostgreSQL ident
actually might be peer
. That's OK also.
对于较新版本的 PostgreSQLident
实际上可能是peer
. 那也没关系。
Inside the psql
shell you can give the DB userpostgres
a password:
在psql
shell 中,您可以给DB 用户postgres
一个密码:
ALTER USER postgres PASSWORD 'newPassword';
You can leave the psql
shell by typing CtrlDor with the command \q
.
您可以psql
通过键入CtrlD或使用命令离开shell \q
。
Now you should be able to give pgAdmin a valid password for the DB superuser and it will be happy too. :-)
现在您应该能够为 pgAdmin 提供数据库超级用户的有效密码,它也会很高兴。:-)
回答by Diego
The response of staff is correct, but if you want to further automate can do:
工作人员的反应是正确的,但是如果你想进一步自动化可以这样做:
$ sudo -u postgres psql -c "ALTER USER postgres PASSWORD 'postgres';"
$ sudo -u postgres psql -c "ALTER USER postgres PASSWORD 'postgres';"
Done! You saved User = postgres and password = postgres.
完毕!您保存了用户 = postgres 和密码 = postgres。
If you do not have a password for the User postgres ubuntu do:
如果您没有用户 postgres ubuntu 的密码,请执行以下操作:
$ sudo passwd postgres
$ sudo passwd postgres
回答by Miguel Pereira
This was frustrating, most of the above answers are correct but they fail to mention you have to restart the database servicebefore the changes in the pg_hba.conf file will take affect.
这令人沮丧,上述大多数答案都是正确的,但他们没有提到您必须在 pg_hba.conf 文件中的更改生效之前重新启动数据库服务。
so if you make the changes as mentioned above:
因此,如果您进行上述更改:
local all postgres ident
then restartas root ( on centos its something like service service postgresql-9.2 restart ) now you should be able to access the db as the user postgres
然后以 root 身份重新启动(在 centos 上它类似于 service service postgresql-9.2 restart )现在您应该能够以用户 postgres 的身份访问数据库
$psql
psql (9.2.4)
Type "help" for help.
postgres=#
Hope this adds info for new postgres users
希望这会为新的 postgres 用户添加信息
回答by stevek
Edit the pg_hba.conf file, e.g. with sudo emacs /etc/postgresql/9.3/main/pg_hba.conf
编辑 pg_hba.conf 文件,例如 sudo emacs /etc/postgresql/9.3/main/pg_hba.conf
Change all authentication methods to trust
. Change Unix Password for "postgres" user. Restart Server. Login with psql -h localhost -U postgres
and use the just set Unix password. If it works you can re-set the pg_hba.conf file to the default values.
将所有身份验证方法更改为trust
. 更改“postgres”用户的 Unix 密码。重启服务器。登录psql -h localhost -U postgres
并使用刚刚设置的 Unix 密码。如果可行,您可以将 pg_hba.conf 文件重新设置为默认值。
回答by Jose Kj
For those who are using it first time and have no information regarding what the password is they can follow the below steps(assuming you are on ubuntu):
对于那些第一次使用它并且不知道密码是什么的人,他们可以按照以下步骤操作(假设您使用的是 ubuntu):
Open the file pg_hba.confin
/etc/postgresql/9.x/main
sudo vi pg_hba.conf
2.edit the below line
local all postgres peer
to
local all postgres trust
Restart the server
sudo service postgresql restart
Finally you can login without need of a password as shown in the figure
打开文件的pg_hba.conf中
/etc/postgresql/9.x/main
sudo vi pg_hba.conf
2.编辑下面的行
local all postgres peer
到
local all postgres trust
重启服务器
sudo service postgresql restart
回答by geekgugi
If you are trying to login postgres shell as postgres user, then you can use following commands.
如果您尝试以 postgres 用户身份登录 postgres shell,则可以使用以下命令。
switch to postgres user
切换到 postgres 用户
# su - postgres
login to psql
登录到 psql
# psql
Hope that helps
希望有帮助
回答by Francisco Puga
Try to not use the -W parameter and leave the password in blank. Sometimes the user is created with no-password.
尽量不要使用 -W 参数并将密码留空。有时用户是使用无密码创建的。
If that doesn't work reset the password. There are several ways to do it, but this works on many systems:
如果这不起作用,请重置密码。有几种方法可以做到这一点,但这适用于许多系统:
$ su root
$ su postgres
$ psql -h localhost
> ALTER USER postgres with password 'YourNewPassword';
回答by user12023415
Once you are in your postgres shell, Enter this command
进入 postgres shell 后,输入此命令
postgres=# \password postgres
After entering this command you will be prompted to set your password , just set the password and then try.
输入这个命令后会提示你设置密码,直接设置密码然后试试。
回答by ardilgulez
As a rule of thumb: YOU SHOULD NEVER EVER SET A PASSWORD FOR THE POSTGRES USER.
根据经验:您永远不应该为 POSTGRES 用户设置密码。
If you need a superuser access from pgAdmin, make another superuser. That way, if the credentials for that superuser is compromised, you can always ssh into the actual database host and manually delete the superuser using
如果您需要来自 pgAdmin 的超级用户访问权限,请创建另一个超级用户。这样,如果该超级用户的凭据遭到破坏,您始终可以通过 ssh 连接到实际的数据库主机并使用以下命令手动删除超级用户
sudo -u postgres -c "DROP ROLE superuser;"
回答by Loaderon
When you install postgresql no password is set for user postgres, you have to explicitly set it on Unix by using the command:
当您安装 postgresql 时,没有为用户 postgres 设置密码,您必须使用以下命令在 Unix 上显式设置它:
sudo passwd postgres
It will ask your sudo password and then promt you for new postgres user password. Source
它会询问您的 sudo 密码,然后提示您输入新的 postgres 用户密码。 来源