By default PostgreSQL only accepts connections from localhost. Enabling remote connections requires changes to both
postgresql.conf AND pg_hba.conf AND Windows Firewall. All three must be configured.Three steps
Enable remote connections step by step
- 1
Edit postgresql.conf — set listen_addresses
Open
postgresql.confin the data directory and changelisten_addresses:# Find this line (remove the # if commented out):#listen_addresses = 'localhost'# Change to listen on all interfaces:listen_addresses = '*'# Or listen on a specific IP only:listen_addresses = '192.168.1.10'Using*listens on all network interfaces. For production, specify only the IP address clients will connect to. - 2
Edit pg_hba.conf — allow remote clients
Open
pg_hba.confand add a rule for your client IP range:# Allow specific user from a subnet:host all all 192.168.1.0/24 scram-sha-256# Allow all users from anywhere (not recommended for production):host all all 0.0.0.0/0 scram-sha-256# Allow specific user to specific database from specific IP:host mydb myuser 10.0.0.5/32 scram-sha-256 - 3
Open Windows Firewall for port 5432
PS> New-NetFirewallRule -DisplayName "PostgreSQL 5432" -Direction Inbound -Protocol TCP -LocalPort 5432 -Action Allow# Or restrict to specific source IP:PS> New-NetFirewallRule -DisplayName "PostgreSQL 5432" -Direction Inbound -Protocol TCP -LocalPort 5432 -RemoteAddress 192.168.1.0/24 -Action Allow - 4
Restart PostgreSQL to apply all changes
C:\> net stop postgresql-x64-18 && net start postgresql-x64-18 - 5
Test from a remote machine
C:\> psql -h 192.168.1.10 -U postgres -d mydbPassword for user postgres:mydb=#
Troubleshoot
Connection refused or timeout
| Symptom | Likely cause | Fix |
|---|---|---|
| Connection refused | listen_addresses still localhost | Check postgresql.conf, restart service |
| Connection timed out | Windows Firewall blocking | Add inbound rule for port 5432 |
| Password authentication failed | pg_hba.conf missing rule | Add host rule for client IP in pg_hba.conf |
| No pg_hba.conf entry | pg_hba.conf rule missing or wrong IP | Add correct host rule and reload config |
FAQ
Remote connection questions
Changes made but still cannot connect remotely
Check all three pieces: (1)
netstat -ano | findstr 5432 should show 0.0.0.0:5432 not 127.0.0.1:5432 after the restart. (2) pg_hba.conf must have a matching host rule. (3) Windows Firewall inbound rule must exist for port 5432.Is it safe to use 0.0.0.0/0 in pg_hba.conf?
Only if you also have strong authentication (scram-sha-256) and strong passwords. For production, restrict to specific IP ranges. A database accessible from anywhere with a weak password is a serious security risk.
How do I reload pg_hba.conf without restarting?
pg_hba.conf changes can be applied without a full restart:
psql -U postgres -c "SELECT pg_reload_conf();". postgresql.conf changes to listen_addresses still require a full service restart.