81

Hi I am having trouble with postgres. I don't remember my postgres password and don't know how to change the password. I'm guessing I should change the md5 password settings I set a month ago, but I don't know how to find the file and open it using my terminal. Can someone help?

3 Answers 3

127

Another way I learned recently is to go to the terminal and type:

ps aux | grep postgres

which shows all the postgres processes running on your machine. From the list you should see one with the format ... -D .... E.G:

root            4155   0.0  0.0  2432908     68   ??  S     6May13   0:00.01 sudo su postgres -c /opt/local/lib/postgresql84/bin/postgres -D /opt/local/var/db/postgresql84/defaultdb -p 5432

the -D means directory. In the terminal, do a sudo su and then cd to that directory, and you'll find the pg_hba.conf file.

And one more way:

Go to your terminal and type: locate pg_hba.conf. There should be a few results.

3
  • 1
    Thanks helpful .. link for future.. Mar 30, 2015 at 8:22
  • 5
    In latest Postgres 9.x it will be found in /data/ directory like /Library/PostgreSQL/9.4/data/pg_hba.conf
    – UserBSS1
    Feb 1, 2016 at 15:04
  • 1
    In my MacBook Air 11.2.3 (2020) - Postgres 10 /Library/PostgreSQL/10/share/postgresql/pg_hba.conf.sample Feb 11, 2022 at 12:41
86

If you can connect, use SHOW hba_file;.

If you cannot connect, you need to locate the data directory. That'll be shown as the -D argument to the postgres or pg_ctl command that starts PostgreSQL, so you can generally find it with ps -ef | grep postgres.

4
  • 8
    Example result on my Mountain Lion Mac with Postgres 9.2 installed by EnterpriseDB.com installer: /Library/PostgreSQL/9.2/data/pg_hba.conf. Feb 12, 2013 at 9:58
  • 7
    For mac 10.11, postgres 11.2 -> /usr/local/var/postgres/pg_hba.conf
    – Vivek
    Feb 25, 2019 at 14:59
  • Thanks @BasilBourque I found the data directory but cannot access that. Error of having no permission to read data content prompted.
    – Shark Deng
    Sep 18, 2019 at 4:04
  • 1
    @SharkDeng For enhanced security, that directory is controlled by the user account created on your system when you installed Postgres. That user is likely called postgres unless you specified otherwise. Sep 18, 2019 at 5:23
1

For macOS 12, you can open the file using nano in your terminal. Example below is if Postgres 12 is installed.

`nano /Library/PostgreSQL/12/data/pg_hba.conf`
1
  • 1
    Different user can have different paths, find yours like this: sudo psql -U postgres -c 'show hba_file' Apr 14 at 11:19

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.