2

I am trying to get into my postgres shell to manage my database and I am running into an issue getting into my postgres shell.

omars-mbp:postgres omarjandali$ brew services restart postgres
Stopping `postgresql`... (might take a while)
==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
omars-mbp:postgres omarjandali$ psql
Password for user omarjandali: 
psql: error: could not connect to server: FATAL:  password authentication failed for user "omarjandali"

I also tried the default psql postgres user:

omars-mbp:postgres omarjandali$ psql -U postgres
Password for user postgres: 
psql: error: could not connect to server: FATAL:  password authentication failed for user "postgres"
omars-mbp:postgres omarjandali$ 

Is there a way for me to reset the password. I don't remember setting a master password.

1
  • Hi, if my answer solved your problem, please consider accepting it.Thanks Commented Jul 31, 2020 at 15:37

2 Answers 2

6

Step 1. Backup the pg_hba.conf file by copying it to a different location, or just copy it to e.g. pg_hba.conf.bk

Step 2. Edit the pg_dba.conf file and change the "method" (last column, could be "md5", "scram-sha-256", etc.) for all local connections to "trust".

Step 3. Restart the PostgreSQL server (Service).

Step 4. Connect to PostgreSQL database server using any tool such as psql or pgAdmin:

psql -U postgres

PostgreSQL will not require a password to login.

Step 5. Execute the following command to set a new password for the postgres user.

ALTER USER postgres WITH PASSWORD 'new_password';

Courtesy of PostgreSQLTutorial

Sign up to request clarification or add additional context in comments.

Comments

0

This doesn't directly answer the original question (how to reset/change a password), but if you're trying to do some basic db management such as granting/revoking privileges, etc. it's possible you don't need a password for the postgres user at all.

If you're running PostgreSQL on a *nix-based system you might be able to access the database with only your normal user password by using the sudo command.

For example (tested on Ubuntu 22.04, w/PostgreSQL 16):

$ sudo -u postgres psql
[sudo] password for my_linux_user: 
psql (16.2 (Ubuntu 16.2-1.pgdg22.04+1))
Type "help" for help.

postgres=# 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.