Postgresql – ALTER USER postgres PASSWORD null; now has locked me out of psql

password-recoverypg-hba.confpostgresql

I have installed postgresql version 9.3.14 on High Sierra via HomeBrew. Upon trying to access the server via psql, I was being prompted for a password for the user postgres. Using postgres as the password got me into the database, but I was confused, as I have never been prompted for a password for the default user before. I looked around online on how to circumvent this, and I tried the solution suggested by lalligood on remove password requirement for user postgres which was to run alter user postgres password null;
and now I cannot access psql. Trying postgres, null or omitting a password does not get me in, and I do not know what to do.

Best Answer

Check your PostgreSQL pg_hba.conf. It should have lines like this copied directly from the docs.

# Allow any user on the local system to connect to any database with
# any database user name using Unix-domain sockets (the default for local
# connections).
#
# TYPE  DATABASE        USER            ADDRESS                 METHOD
local   all             all                                     trust

# The same using local loopback TCP/IP connections.
#
# TYPE  DATABASE        USER            ADDRESS                 METHOD
host    all             all             127.0.0.1/32            trust

You don't have to keep these. But it's normal to have these so you can connect locally without having to worry about passwords using your local system authentication methods. You also don't need both of these. If you only ever connect over sockets, feel free to nuke the one with tcp. This will allow you to log in with any locally authenticated user.

So you can log in as PostgreSQL with,

sudo -u postgresql psql -d myDatabase

or using tcp

sudo -u postgresql psql -d myDatabase -h 127.0.0.1
Related Question