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

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

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

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


当前回答

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

在终端上用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中的特定行并保存它。

其他回答

如果你在macOS上运行PostgreSQL,试试这些:

Edit the pg_hba.conf file sudo vi /Library/PostgreSQL/9.2/data/pg_hba.conf Change the "md5" method for all users to "trust" near the bottom of the file Find the name of the service ls /Library/LaunchDaemons Look for postgresql Stop the postgresql service sudo launchctl stop com.edb.launchd.postgresql-9.2 Start the postgresql service sudo launchctl start com.edb.launchd.postgresql-9.2 Start a psql session as postgres psql -U postgres (shouldn't ask for password because of 'trust' setting) Reset password in the psql session by typing: ALTER USER postgres with password 'secure-new-password'; \q Enter Edit the pg_hba.conf file Switch it back to 'md5' Restart services again

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

net user postgres postgres

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

注意:在Linux上,您可以简单地运行sudo su - postgres来成为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中的特定行并保存它。

pg_hba.conf (C:\Program Files\PostgreSQL\9.3\data)文件在给出这些答案后发生了变化。在Windows中,对我有用的是打开文件并将METHOD从md5更改为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

然后,我使用pgAdmin III在没有密码的情况下登录,通过“文件→更改密码”菜单修改了用户postgres的密码。