我忘记或者在安装过程中输入了PostgreSQL默认用户的密码。我似乎不能运行它,我得到以下错误:

psql: FATAL:  password authentication failed for user "hisham"
hisham-agil: hisham$ psql

是否有方法重置密码或如何创建具有超级用户权限的新用户?

我是PostgreSQL的新手,只是第一次安装它。我正在尝试使用Ruby on Rails,我正在运行Mac OS X v10.7 (Lion)。


当前回答

按照步骤1找到最佳答案。

如果你使用Windows操作系统,这是我的补充。只遵循第1步,然后在web上打开pgAdmin或postgres,并单击顶部导航上的file。单击reset layout,最后重新加载应用程序。不管你输入什么密码都可以。我用的是1234。

其他回答

我只是在Windows 10上有这个问题,在我的情况下,问题是我只是运行psql,它默认试图登录与我的Windows用户名(“内森”),但没有PostgreSQL用户的名字,它没有告诉我。

所以解决方案是运行psql -U postgres而不是仅仅运行psql,然后我在安装时输入的密码就可以工作了。

我这么做是为了解决同样的问题:

在终端上用gedit编辑器打开pg_hba.conf文件:

sudo gedit /etc/postgresql/9.5/main/pg_hba.conf

它会要求输入密码。输入admin登录密码。 这将打开gedit文件。粘贴如下一行:

host  all   all  127.0.0.1/32  trust
just below -

# Database administrative login by Unix domain socket

保存并关闭它。

关闭终端,重新打开并运行如下命令:

psql -U postgres

现在您将进入psql控制台。

现在输入这个来修改密码:

ALTER USER [your preferred user name] with password '[desired password]';

如果它说用户不存在,那么使用CREATE而不是ALTER。

最后,删除粘贴到pg_hba中的特定行并保存它。

按照步骤1找到最佳答案。

如果你使用Windows操作系统,这是我的补充。只遵循第1步,然后在web上打开pgAdmin或postgres,并单击顶部导航上的file。单击reset layout,最后重新加载应用程序。不管你输入什么密码都可以。我用的是1234。

当从命令行连接到PostgreSQL时,不要忘记添加-h localhost作为命令行参数。如果不是,PostgreSQL将尝试使用PEER认证模式进行连接。

下面显示了密码重置、使用PEER身份验证登录失败和使用TCP连接登录成功。

# sudo -u postgres psql

could not change directory to "/root"
psql (9.1.11)
Type "help" for help.

postgres=# \password
Enter new password:
Enter it again:
postgres=# \q

失败:

# psql -U postgres -W

Password for user postgres:
psql: FATAL:  Peer authentication failed for user "postgres"

使用-h localhost:

# psql -U postgres -W  -h localhost

Password for user postgres:
psql (9.1.11)
SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
Type "help" for help.

postgres=#

Edit the file /etc/postgresql/<version>/main/pg_hba.conf and find the following line: local all postgres md5 Edit the line and change md5 at the end to trust and save the file Reload the postgresql service sudo service postgresql reload This will load the configuration files. Now you can modify the postgres user by logging into the psql shell psql -U postgres Update the postgres user's password alter user postgres with password 'secure-passwd-here'; Edit the file /etc/postgresql/<version>/main/pg_hba.conf and change trust back to md5 and save the file Reload the postgresql service sudo service postgresql reload Verify that the password change is working psql -U postgres -W