Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL: Resetting password of PostgreSQL on Ubuntu [closed]

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.

like image 673
Ritesh Mehandiratta Avatar asked Jan 29 '13 17:01

Ritesh Mehandiratta


People also ask

What is the default password for postgres in Ubuntu?

For most systems, the default Postgres user is postgres and a password is not required for authentication. Thus, to add a password, we must first login and connect as the postgres user. If you successfully connected and are viewing the psql prompt, jump down to the Changing the Password section.

Where is postgres password stored?

PostgreSQL database passwords are separate from operating system user passwords. The password for each database user is stored in the pg_authid system catalog. Passwords can be managed with the SQL commands CREATE ROLE and ALTER ROLE, e.g., CREATE ROLE foo WITH LOGIN PASSWORD 'secret' , or the psql command \password .


1 Answers

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.conf file (in the /etc/postgresql/9.1/main directory), it should contain this line as the first rule:

# Database administrative login by Unix domain socket   local   all             postgres                                peer 

(About the file location: 9.1 is the major postgres version and main the name of your "cluster". It will differ if using a newer version of postgres or non-default names. Use the pg_lsclusters command to obtain this information for your version/system).

Anyway, if the pg_hba.conf file does not have that line, edit the file, add it, and reload the service with sudo service postgresql reload.

Then you should be able to log in with psql as the postgres superuser with this shell command:

sudo -u postgres psql 

Once inside psql, issue the SQL command:

ALTER USER postgres PASSWORD 'newpassword'; 

In this command, postgres is the name of a superuser. If the user whose password is forgotten was ritesh, the command would be:

ALTER USER ritesh PASSWORD 'newpassword'; 

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:

\password username 

It asks for the password with a double blind input, then hashes it according to the password_encryption setting and issue the ALTER USER command to the server with the hashed version of the password, instead of the clear text version.

like image 87
Daniel Vérité Avatar answered Sep 30 '22 18:09

Daniel Vérité