Skip to content

How to Fix PostgreSQL 'no pg_hba.conf entry' Error

DodaTech 2 min read

In this tutorial, you'll learn about How to Fix PostgreSQL 'no pg_hba.conf entry' Error. We cover key concepts, practical examples, and best practices.

The Problem

You try to connect to PostgreSQL and get:

FATAL: no pg_hba.conf entry for host "192.168.1.50", user "app", database "mydb", SSL off

PostgreSQL's pg_hba.conf file controls which hosts, users, databases, and authentication methods are allowed. By default, PostgreSQL only listens on localhost and rejects remote connections. The error tells you exactly which host, user, and database were rejected, making it straightforward to add the correct rule.

Quick Fix

1. Find the pg_hba.conf location

# If you know the data directory
ls /etc/postgresql/*/main/pg_hba.conf

# Or ask PostgreSQL
sudo -u postgres psql -c "SHOW hba_file;"

Expected output:

              hba_file
------------------------------------
 /etc/postgresql/16/main/pg_hba.conf

2. Allow remote connections

Edit pg_hba.conf and add:

# TYPE  DATABASE  USER        ADDRESS          METHOD
host    all       all         192.168.1.0/24   scram-sha-256

This allows any user on the 192.168.1.0/24 subnet to connect to any database using password authentication. Replace scram-sha-256 with md5 for older clients.

3. Allow Docker container connections

# Allow all Docker containers (172.17.0.0/16)
host    all       all         172.17.0.0/16    scram-sha-256

If you're running your app in Docker, the container's IP will be in the Docker bridge network range. Use the appropriate subnet for host-network or compose networks.

4. Allow local socket connections without password

# TYPE  DATABASE  USER        METHOD
local   all       postgres    peer
local   all       app         md5

peer trusts the OS username. md5 or scram-sha-256 require a password. For development only, you can use trust to skip authentication.

5. Reload PostgreSQL configuration

sudo systemctl reload postgresql

Or from within PostgreSQL:

SELECT pg_reload_conf();

Expected output:

 pg_reload_conf
----------------
 t

6. Also update postgresql.conf to listen on all addresses

# Edit postgresql.conf (same directory as pg_hba.conf)
listen_addresses = '*'

Without this, PostgreSQL won't listen on external network interfaces even if pg_hba.conf allows the connection.

7. Verify the connection

psql -h 192.168.1.100 -U app -d mydb -W

Expected output:

Password:
psql (16.4)
Type "help" for help.

mydb=>

8. Test authentication method availability

# Check if scram-sha-256 is available
psql -U postgres -c "SELECT * FROM pg_available_extensions WHERE name = 'pgcrypto';"

Some authentication methods require extensions. If your client only supports md5 but the server enforces scram-sha-256, you may need to update the client library or change pg_hba.conf to use md5 for that host.

9. Use hostssl for encrypted-only connections

# TYPE   DATABASE  USER        ADDRESS          METHOD
hostssl  all       all         192.168.1.0/24   scram-sha-256
hostnossl all      all         0.0.0.0/0        reject

Use hostssl to require SSL connections and hostnossl with reject to block unencrypted connections. This adds a security layer when clients connect over untrusted networks.

Prevention

  • Use specific IP ranges instead of 0.0.0.0/0 for security
  • Prefer scram-sha-256 over md5 for PostgreSQL 10+
  • Restart is not needed — pg_reload_conf() or sudo systemctl reload <a href="/databases/postgresql/">postgresql</a> is sufficient
  • Remember to update both pg_hba.conf AND <a href="/databases/postgresql/">postgresql</a>.conf (listen_addresses)
  • Document which IP ranges are allowed for each environment (dev, staging, prod)
  • Place more specific rules before less specific ones — pg_hba.conf processes rules in order

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro