Postgresql: How to find pg_hba.conf file using Mac OS X

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?

107276 次浏览

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.

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.

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`