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

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

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

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


当前回答

当从命令行连接到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=#

其他回答

对于Windows安装,将创建Windows用户。“psql”使用该用户连接到端口。如果您更改了PostgreSQL用户的密码,它不会更改Windows用户的密码。 只有当您可以访问命令行时,下面的命令行才能工作。

相反,您可以使用Windows GUI应用程序“c:\Windows\system32\lusrmgr.exe”。这个应用程序管理由Windows创建的用户。现在可以修改密码了。

如果你在Windows上,你可以直接运行

net user postgres postgres

并以postgres/postgres用户/密码登录PostgreSQL。

注意:在Linux上,您可以简单地运行sudo su - postgres来成为postgres用户,然后使用psql更改所需的内容。

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

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

Windows用户PostgreSQL最新版本(大于10):

到PostgreSQL安装位置,搜索pg_hba.conf,你会在..\postgres\data\pg_hba.conf中找到它。

用记事本打开这个文件,找到这行:

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
#..

Change the method from *md5* to *trust*:

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust
# ...

现在转到你的SQL shell (PSQL),让一切都为空,

Server [localhost]:
Database [postgres]:
Port [8000]:
Username [postgres]:

这次它不会要求你输入密码,你就可以登录了,

现在运行这一行:

  `ALTER USER yourusername WITH SUPERUSER`

现在您可以将\q留在外壳中。

再次进入“pg_hba.conf”文件,将“METHOD”从“trust”修改为“md5”并保存。

现在使用新用户和密码登录,可以检查\du的属性。