我忘记或者在安装过程中输入了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)。
当前回答
如果你在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
其他回答
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文件(正式文档)
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
我没能在C:\Program Files\PostgreSQL\14\ data文件夹中找到pg_hba.conf文件,因为根本没有文件夹数据。
我通过使用pgAdmin创建一个新用户并赋予其超级系统管理员权限来解决这个问题。
注意:在Linux上,您可以简单地运行sudo su - postgres来成为postgres用户,然后使用psql更改所需的内容。
如果你在Windows上,你可以直接运行
net user postgres postgres
并以postgres/postgres用户/密码登录PostgreSQL。