PostgreSQL 13用户postgres密码认证失败,求有效解决方案
Hey there, I’ve run into this exact issue before—let’s get you connected properly. The root cause here is how your pg_hba.conf is configured, and it’s why your password isn’t being recognized at all.
Why Your Password Isn’t Working
Looking at your pg_hba.conf, all local connections (Unix socket, IPv4/IPv6 localhost) are set to use the peer authentication method. This method doesn’t care about passwords at all—it only checks if your system username matches the PostgreSQL username you’re trying to log in as. So even if you enter the correct password, PostgreSQL ignores it, leading to the "password authentication failed" error. The GSSAPI context error is a side effect of the client trying alternative auth methods that the server isn’t configured to accept.
Step-by-Step Fix
Update your
pg_hba.conffile
Open/Library/PostgreSQL/13/data/pg_hba.confin a text editor (you’ll need sudo privileges, e.g.,sudo nano /Library/PostgreSQL/13/data/pg_hba.conf). Change all instances ofpeertoscram-sha-256(PostgreSQL 13’s recommended secure auth method) ormd5if you prefer:# "local" is for Unix domain socket connections only local all all scram-sha-256 # IPv4 local connections: host all all 127.0.0.1/32 scram-sha-256 # IPv6 local connections: host all all ::1/128 scram-sha-256 # Allow replication connections from localhost, by a user with the # replication privilege. local replication all scram-sha-256 host replication all 127.0.0.1/32 scram-sha-256 host replication all ::1/128 scram-sha-256Restart PostgreSQL to apply changes
On macOS, you can restart the service using the PostgreSQL app’s built-in restart button, or run this command in the terminal:sudo /Library/PostgreSQL/13/bin/pg_ctl restart -D /Library/PostgreSQL/13/dataVerify/reset your postgres user password (if needed)
If you’re still unsure about your password, you can reset it easily using the peer auth method while it’s still active (before restarting, or temporarily revert if you already restarted):- Open a terminal and run:
sudo -u postgres psql(this logs you into PostgreSQL as the postgres user without a password, thanks to peer auth) - Reset the password with:
ALTER USER postgres WITH ENCRYPTED PASSWORD 'your-secure-password-here'; - Exit with
\q, then restart PostgreSQL as before.
- Open a terminal and run:
Why This Works
By switching to scram-sha-256 or md5, PostgreSQL will now prompt for and validate the password you set. The GSSAPI error will disappear because the client will use the password-based auth method you’ve configured instead of trying to initiate a GSSAPI context that the server doesn’t support.
内容的提问来源于stack exchange,提问作者roe

