我忘记或者在安装过程中输入了PostgreSQL默认用户的密码。我似乎不能运行它,我得到以下错误:
psql: FATAL: password authentication failed for user "hisham"
hisham-agil: hisham$ psql
是否有方法重置密码或如何创建具有超级用户权限的新用户?
我是PostgreSQL的新手,只是第一次安装它。我正在尝试使用Ruby on Rails,我正在运行Mac OS X v10.7 (Lion)。
我忘记或者在安装过程中输入了PostgreSQL默认用户的密码。我似乎不能运行它,我得到以下错误:
psql: FATAL: password authentication failed for user "hisham"
hisham-agil: hisham$ psql
是否有方法重置密码或如何创建具有超级用户权限的新用户?
我是PostgreSQL的新手,只是第一次安装它。我正在尝试使用Ruby on Rails,我正在运行Mac OS X v10.7 (Lion)。
Find the file pg_hba.conf. It may be located, for example, in /etc/postgresql-9.1/pg_hba.conf. cd /etc/postgresql-9.1/ Back it up cp pg_hba.conf pg_hba.conf-backup Place the following line (as either the first uncommented line, or as the only one): For all occurrence of below (local and host) , except replication section if you don't have any it has to be changed as follow ,no MD5 or Peer authentication should be present. local all all trust Restart your PostgreSQL server (e.g., on Linux:) sudo /etc/init.d/postgresql restart If the service (daemon) doesn't start reporting in log file: local connections are not supported by this build you should change local all all trust to host all all 127.0.0.1/32 trust You can now connect as any user. Connect as the superuser postgres (note, the superuser name may be different in your installation. In some systems it is called pgsql, for example.) psql -U postgres or psql -h 127.0.0.1 -U postgres (note that with the first command you will not always be connected with local host) Reset the password ('replace my_user_name with postgres since you are resetting the postgres user) ALTER USER my_user_name with password 'my_secure_password'; Restore the old pg_hba.conf file as it is very dangerous to keep around cp pg_hba.conf-backup pg_hba.conf Restart the server, in order to run with the safe pg_hba.conf file sudo /etc/init.d/postgresql restart
关于pg_hba文件的进一步阅读:19.1。pg_hba.conf文件(正式文档)
当从命令行连接到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=#
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的密码。
如果你在Windows上,你可以直接运行
net user postgres postgres
并以postgres/postgres用户/密码登录PostgreSQL。
对于Windows安装,将创建Windows用户。“psql”使用该用户连接到端口。如果您更改了PostgreSQL用户的密码,它不会更改Windows用户的密码。 只有当您可以访问命令行时,下面的命令行才能工作。
相反,您可以使用Windows GUI应用程序“c:\Windows\system32\lusrmgr.exe”。这个应用程序管理由Windows创建的用户。现在可以修改密码了。
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
我这么做是为了解决同样的问题:
在终端上用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中的特定行并保存它。
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的属性。
我只是在Windows 10上有这个问题,在我的情况下,问题是我只是运行psql,它默认试图登录与我的Windows用户名(“内森”),但没有PostgreSQL用户的名字,它没有告诉我。
所以解决方案是运行psql -U postgres而不是仅仅运行psql,然后我在安装时输入的密码就可以工作了。
如果你在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(这对我很有帮助):
这就是我要参考的文档:如何重置PostgreSQL密码?
Open your cmd and go to C:\Program Files\PostgreSQL\12\data. This is usually the right path. You might have it stored somewhere else. Note that, if you have a different PostgreSQL version, there will be a different number. That doesn't matter. Find a pg_hba.conf file and copy it to somewhere else (that way you will have an unmodified version of this file, so you will be able to look at it after we make some changes) Open the pg_hba.conf file (not the backup, but the original) Find the multiple lines that start with host near the bottom of the file: host all all 127.0.0.1/32 md5 host all all ::1/128 md5 host replication all 127.0.0.1/32 md5 host replication all ::1/128 md5 Replace md5 with trust: host all all 127.0.0.1/32 trust host all all ::1/128 trust host replication all 127.0.0.1/32 trust host replication all ::1/128 trust Close this file Go to your search bar on windows and open Services app. Find postgres and restart it. Picture of services app Write cd.. in cmd and then cd bin. Your path should be C:\Program Files\PostgreSQL\12\bin Enter: psql -U postgres -h localhost Enter: ALTER USER postgres with password '<your new password>';Make sure that you include ; at the end “ALTER ROLE” should be displayed as an indication that the previous line was executed successfully Open original pg_hba.conf file and change back from trust to md5 Restart the server with Services app as before
将下面的代码添加到pg_hba.conf文件中。PostgreSQL的安装目录中会出现哪个
hostnossl all all 0.0.0.0/0 trust
它将开始工作。
按照步骤1找到最佳答案。
如果你使用Windows操作系统,这是我的补充。只遵循第1步,然后在web上打开pgAdmin或postgres,并单击顶部导航上的file。单击reset layout,最后重新加载应用程序。不管你输入什么密码都可以。我用的是1234。
我没能在C:\Program Files\PostgreSQL\14\ data文件夹中找到pg_hba.conf文件,因为根本没有文件夹数据。
我通过使用pgAdmin创建一个新用户并赋予其超级系统管理员权限来解决这个问题。