Windows10下PostgreSQL9.6 psql连接失败:pg_hba.conf无::1条目求助
Hey there! Let's work through this connection issue together. I've seen this pop up even when folks think they've configured pg_hba.conf correctly, so let's check a few key things:
1. Double-check you're editing the correct pg_hba.conf file
On Windows 10, PostgreSQL 9.6 stores its data directory (where pg_hba.conf lives) by default at C:\Program Files\PostgreSQL\9.6\data\pg_hba.conf. It's easy to accidentally edit a backup or a copy in another folder—confirm you're modifying the file in the active data directory of your running PostgreSQL instance.
2. Ensure your pg_hba.conf syntax is valid
Looking at your provided config, the line IPv6 local connections: isn't commented out. PostgreSQL will treat this as an invalid configuration line, which can cause it to ignore the rules that follow it. Update that section to:
# IPv6 local connections: host all all ::1/128 trust host all all ::0/0 trust
The # turns it into a comment, so PostgreSQL parses the actual rules correctly.
3. Restart the PostgreSQL service after making changes
Editing pg_hba.conf doesn't take effect until you restart the PostgreSQL service. Here's how to do it:
- Press Win + R, type
services.mscand hit Enter - Find the service named
postgresql-x64-9.6(or similar, depending on your installation) - Right-click it and select Restart
4. Verify listen_addresses in postgresql.conf
Another common gotcha is the listen_addresses setting in postgresql.conf (also in the data directory). Make sure it's set to:
listen_addresses = '*'
This tells PostgreSQL to listen on all available IPv4 and IPv6 addresses. If it's set to localhost only, it might not respond to the IPv6 loopback (::1) even if pg_hba.conf allows it. Don't forget to restart the service after changing this too!
5. Test the connection explicitly
Try connecting with a specific host flag to narrow down the issue:
- For IPv4:
psql -U postgres -d postgres -h 127.0.0.1 - For IPv6:
psql -U postgres -d postgres -h ::1
If one works and the other doesn't, it'll help pinpoint whether the problem is with IPv6 configuration specifically.
Give these steps a shot—most of the time, it's either a missing service restart, a syntax error in pg_hba.conf, or pointing to the wrong config file.
内容的提问来源于stack exchange,提问作者krithikaGopalakrishnan

