Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change PostgreSQL user password?

How do I change the password for PostgreSQL user?

like image 222
Saad Avatar asked Oct 04 '12 05:10

Saad


People also ask

How do I change my password in postgres?

Log in to psql using the postgres database login role, connecting to the postgres database. Issue the \password command to alter the passwords of the three login roles. The syntax for the \password command is \password <username>. You will be prompted to type a new password.

How do I change user privileges in PostgreSQL?

First, connect to your database cluster as the admin user, doadmin , by passing the cluster's connection string to psql . This brings you into the interactive shell for PostgreSQL, which changes your command prompt to defaultdb=> . From here, connect to the database that you want to modify the user's privileges on.


2 Answers

To log in without a password:

sudo -u user_name psql db_name 

To reset the password if you have forgotten:

ALTER USER user_name WITH PASSWORD 'new_password'; 
like image 93
solaimuruganv Avatar answered Oct 07 '22 03:10

solaimuruganv


To change the the postgres user's password follow this steps

  1. Login into the psql:
$ sudo -u postgres psql 
  1. Then in the psql console change the password and quit:
postgres=# \password postgres Enter new password: <new-password> postgres=# \q 

or using a query

ALTER USER postgres PASSWORD '<new-password>'; 

or in one line

sudo -u postgres psql -c "ALTER USER postgres PASSWORD '<new-password>';" 

Note:

If that does not work, reconfigure authentication by editing /etc/postgresql/9.1/main/pg_hba.conf (path will differ) and change:

local   all         all                  peer # change this to md5  ## to  local   all         all                  md5 # like this 

Then restart the server:

$ sudo service postgresql restart 
like image 40
Clint Bugs Avatar answered Oct 07 '22 02:10

Clint Bugs