PostgreSQL connection errors: refused, password failed, no pg_hba.conf entry, too many clients

A PostgreSQL error at connect time is usually one of a few messages: connection refused, password authentication failed, no pg_hba.conf entry, or too many clients. Each points to a different place, so read the exact wording and the server log first, then use the matching fix below.

Applies to:PostgreSQLAll supported versions (current docs: 18)
On this page

What it means

PostgreSQL has no single "PostgreSQL error". It reports each problem as a message plus a five-character SQLSTATE code, and what you should do depends on which message you got. This page covers the ones people hit when connecting from psql, pgAdmin or an application driver.

The key split is between "nothing answered" and "the server answered and said no". Connection refused means nothing accepted the connection on that host and port. The FATAL: messages mean the server was reached and turned you down for a specific reason. For the full list of codes, see PostgreSQL error codes (SQLSTATE).

Symptoms and causes

You are likely dealing with this problem if:

  • could not connect to server: Connection refused
  • FATAL: password authentication failed for user "x" (28P01)
  • FATAL: no pg_hba.conf entry for host "...", user "...", database "..." (28000)
  • FATAL: database "x" does not exist (3D000), or FATAL: role "x" does not exist
  • FATAL: sorry, too many clients already (53300) or FATAL: the database system is starting up (57P03)

The usual causes, most common first:

Server not running, wrong host or port, or blocked port
For connection refused: the server is down or still starting, you are using the wrong host or port (default 5432, a second cluster may be on 5433), a firewall or cloud security group blocks the port, or in Docker the port is not published or localhost is used where a service name is needed.
Server only listens on localhost
listen_addresses defaults to localhost, so remote clients are refused. It can only be changed at server start.
Wrong password, or hash and method mismatch
For 28P01: the password is wrong or stale, or the stored hash does not fit the method in pg_hba.conf. A password stored as md5 works only with the md5 and password methods. A SCRAM-stored password works with scram-sha-256 and with md5, which switches to SCRAM automatically. Old client libraries without SCRAM support cannot log in with a SCRAM hash. You may also be connecting to the wrong server or database, or a different pg_hba.conf line may match than you expect.
No matching pg_hba.conf rule
The client address, user, database or SSL mode matches no line. The first matching record wins, with no fall-through. Other common reasons: edits were never reloaded, or a hostssl rule is used while the client connects without SSL.
Wrong database or user name
If you give no database name, PostgreSQL uses the user name as the database name, which often does not exist.
Connection limit reached
max_connections is typically 100. Leaking connections, or having no connection pooler, fills it up.

How to fix it

Work through these in order. They start with the quickest, safest checks and move to deeper repairs; stop as soon as the problem is gone.

1.Read the exact message and the server log

Easy

The server log can contain more detail about an authentication failure than the client is shown, and it tells you which of the fixes below applies.

  1. Copy the full error text, including the SQLSTATE code if you have it, and match it against the symptom list above.
  2. Open the PostgreSQL server log on the server and look at the lines around the time of the failed attempt. Log location depends on your distribution and setup.

2.Connection refused: check the server and the port

Easy

Refused means nothing accepted the connection on that host and port, so first confirm the server is up and you are using the right address.

  1. On the server, check that the PostgreSQL service is running with your system's service manager, and read the server log if it failed to start.
  2. Confirm the port. The default is 5432, but a second cluster may use 5433 or another number. If you can get a session on the server, check the configured value, then pass it to psql with -p.
    SQL
    SHOW port;
  3. Try psql with the host and port spelled out.
    Terminal
    psql -h <host> -p <port> -U <user> -d postgres

3.Connection refused from another machine or container

ModerateChanges settings

By default the server listens only on localhost.

Before you start: Back up postgresql.conf before editing. Listening on all addresses exposes the port to your network, so combine it with narrow pg_hba.conf rules.

  1. Find the config file path (it varies by distribution, for example /etc/postgresql/<ver>/main/ on Debian and Ubuntu, /var/lib/pgsql/<ver>/data/ on RHEL).
    SQL
    SHOW config_file;
  2. In postgresql.conf, set listen_addresses to the specific IPs the server should use, or '*' for all.
    Config
    listen_addresses = '*'
  3. Restart the PostgreSQL service. A reload is not enough, because this setting is only read at server start.
  4. Make sure a matching host line exists in pg_hba.conf (see the pg_hba.conf fix below).

4.Fix "no pg_hba.conf entry"

ModerateChanges settings

The server was reached but no rule in pg_hba.conf matches the client address, user, database and SSL mode.

Before you start: Back up pg_hba.conf first. Order matters: the first matching line wins. Do not use a broad trust rule as a shortcut, since it lets anyone matching it in without a password.

  1. Find the file path.
    SQL
    SHOW hba_file;
  2. Add a line for the client's network, user and database, placed so that no earlier line matches first. Adjust the values to your setup.
    Text
    host  mydb  myuser  203.0.113.0/24  scram-sha-256
  3. Reload the configuration without a restart.
    SQL
    SELECT pg_reload_conf();
  4. Check the file for mistakes: a non-null error column marks a bad line.
    SQL
    SELECT * FROM pg_hba_file_rules;
  5. If the line uses hostssl, make sure the client connects with SSL. The user also needs the CONNECT privilege on the database.

5.Fix "password authentication failed"

ModerateChanges settings

The server accepted the connection but rejected the credentials. The cause is a wrong password or a mismatch between the stored hash and the authentication method.

Before you start: Resetting a password changes it for every application that uses the role, so update those too.

  1. Read the server log for the reason, and check that you are connecting to the intended server and database.
  2. Reset the password as a privileged user from inside psql with \password. Avoid ALTER USER name PASSWORD '...': it puts the plaintext in shell and psql history and possibly in the server log (for example with log_statement).
    Text
    \password name
  3. See how each stored password is hashed. A SCRAM-SHA-256$ prefix means SCRAM.
    SQL
    SELECT rolname, left(rolpassword,14) FROM pg_authid;
  4. Compare with the method on the matching pg_hba.conf line. If a legacy client cannot do SCRAM, the main fix is to upgrade its library or driver.
  5. Only as a temporary measure, and only for a user whose stored hash is md5, put a per-user md5 line before the general scram-sha-256 line. If the stored password is SCRAM, the md5 method switches to SCRAM and a legacy client still fails.
    Text
    host all mike .example.com md5
    host all all  .example.com scram-sha-256
  6. To migrate from md5 to SCRAM: confirm all clients support SCRAM, set password_encryption = 'scram-sha-256', have every user set a new password, then change the pg_hba.conf method to scram-sha-256.
  7. If the error looks contradictory (libpq also makes a second, non-SSL attempt), force SSL for the test. This tip comes from Microsoft's Azure guidance.
    Terminal
    export PGSSLMODE=require

6.Fix "database does not exist" or "role does not exist"

Easy

Often the name is simply wrong, or the client defaulted the database name to the user name.

  1. Connect to the default postgres database, which usually exists, and list databases. psql -l lists databases only, not roles. Without -d, the database name defaults to the user name, so a plain psql -l can itself fail with database does not exist.
    Terminal
    psql -d postgres -l
  2. Inside psql, list roles.
    Text
    \du
  3. Pass the database explicitly with -d, or create it if it should exist.
    SQL
    CREATE DATABASE x;

7.Fix "sorry, too many clients already"

ModerateChanges settings

The server has reached max_connections. Finding who holds the connections is better than just raising the limit.

Before you start: Raising max_connections needs a server restart and costs memory. Schedule it for a quiet period.

  1. See how connections are distributed by state (you may need a superuser or reserved connection to get in).
    SQL
    SELECT count(*), state FROM pg_stat_activity GROUP BY 2;
  2. Fix applications that leak connections, and put a pooler such as PgBouncer in front of the database.
  3. Only if needed, raise max_connections in postgresql.conf and restart. Note that superuser_reserved_connections (default 3) is kept free for administrators.

Check that it worked

  • psql -h <host> -p <port> -U <user> -d <database> connects and shows a prompt.
  • SELECT * FROM pg_hba_file_rules; shows no non-null error values.
  • The application connects and the server log shows no new FATAL lines for it.

If it still fails

  • Re-read the server log from the exact time of a failed attempt. It often names the real reason.
  • Confirm that you edited the right files with SHOW config_file; and SHOW hba_file;, and that you restarted (for listen_addresses, max_connections) or reloaded (for pg_hba.conf) as needed.
  • Test from the server itself with psql, then from the client machine. If it works locally but not remotely, the cause is listen_addresses, pg_hba.conf or the firewall.
  • Look up the SQLSTATE in PostgreSQL error codes (SQLSTATE) or the official documentation, and ask the PostgreSQL community with the exact message and version.

Questions

Does a pg_hba.conf problem cause "connection refused"?

No. A missing rule gives the explicit no pg_hba.conf entry error. Connection refused is a lower-level failure where nothing accepted the connection.

Do I need to restart PostgreSQL after editing pg_hba.conf?

No. Reload with SELECT pg_reload_conf();, pg_ctl reload or a HUP signal. On Windows, changes apply to new connections immediately.

Why would a client that worked before fail with a password error?

A password stored as a SCRAM hash cannot be used by an old client library without SCRAM support. The current docs show scram-sha-256 as the password_encryption default, so a newly set password is SCRAM. Upgrade the driver, or follow the md5-to-SCRAM migration steps above.

Sources

  1. Authentication Problems (PostgreSQL 18 docs, 20.16)Official · PostgreSQL Global Development Group
  2. Connections and Authentication (19.3)Official · PostgreSQL Global Development Group
  3. The pg_hba.conf File (20.1)Official · PostgreSQL Global Development Group
  4. Password Authentication (20.5)Official · PostgreSQL Global Development Group
  5. PostgreSQL Error Codes (Appendix A)Official · PostgreSQL Global Development Group
  6. Password authentication failed for user (Azure Database for PostgreSQL flexible server)Official · Microsoft Learn