In Ubuntu, I installed PostgreSQL database and created a superuser for the server.
If I forgot the password of the postgresql superuser, how can I reset it (the password) for that user?
I tried uninstalling it and then installing it again but the previously created superuser is retained.
Assuming you’re the administrator of the machine, Ubuntu has granted you the right to sudo to run any command as any user.
Also assuming you did not restrict the rights in the
pg_hba.conffile (in the/etc/postgresql/9.1/maindirectory), it should contain this line as the first rule:(About the file location:
9.1is the major postgres version andmainthe name of your "cluster". It will differ if using a newer version of postgres or non-default names. Use thepg_lsclusterscommand to obtain this information for your version/system).Anyway, if the
pg_hba.conffile does not have that line, edit the file, add it, and reload the service withsudo service postgresql reload.Then you should be able to log in with
psqlas the postgres superuser with this shell command:Once inside psql, issue the SQL command:
In this command,
postgresis the name of a superuser. If the user whose password is forgotten wasritesh, the command would be:References: PostgreSQL 9.1.13 Documentation, Chapter 19. Client Authentication
Keep in mind that you need to type postgres with a single S at the end
If leaving the password in clear text in the history of commands or the server log is a problem, psql provides an interactive meta-command to avoid that, as an alternative to
ALTER USER ... PASSWORD:It asks for the password with a double blind input, then hashes it according to the
password_encryptionsetting and issue theALTER USERcommand to the server with the hashed version of the password, instead of the clear text version.