How do I change the password for PostgreSQL user?
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.
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.
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';
To change the the postgres user's password follow this steps
$ sudo -u postgres psql
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>';"
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With