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.
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 refusedFATAL: 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), orFATAL: role "x" does not existFATAL: sorry, too many clients already(53300) orFATAL: 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
localhostis used where a service name is needed. - Server only listens on localhost
listen_addressesdefaults tolocalhost, 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 themd5andpasswordmethods. A SCRAM-stored password works withscram-sha-256and withmd5, 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 differentpg_hba.confline 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
hostsslrule 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_connectionsis 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
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.
- Copy the full error text, including the SQLSTATE code if you have it, and match it against the symptom list above.
- 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
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.
- 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.
- 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.SHOW port; - Try psql with the host and port spelled out.
psql -h <host> -p <port> -U <user> -d postgres
3.Connection refused from another machine or container
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.
- 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).SHOW config_file; - In
postgresql.conf, setlisten_addressesto the specific IPs the server should use, or'*'for all.listen_addresses = '*' - Restart the PostgreSQL service. A reload is not enough, because this setting is only read at server start.
- Make sure a matching
hostline exists inpg_hba.conf(see the pg_hba.conf fix below).
4.Fix "no pg_hba.conf entry"
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.
- Find the file path.
SHOW hba_file; - 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.
host mydb myuser 203.0.113.0/24 scram-sha-256 - Reload the configuration without a restart.
SELECT pg_reload_conf(); - Check the file for mistakes: a non-null
errorcolumn marks a bad line.SELECT * FROM pg_hba_file_rules; - If the line uses
hostssl, make sure the client connects with SSL. The user also needs theCONNECTprivilege on the database.
5.Fix "password authentication failed"
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.
- Read the server log for the reason, and check that you are connecting to the intended server and database.
- Reset the password as a privileged user from inside psql with
\password. AvoidALTER USER name PASSWORD '...': it puts the plaintext in shell and psql history and possibly in the server log (for example withlog_statement).\password name - See how each stored password is hashed. A
SCRAM-SHA-256$prefix means SCRAM.SELECT rolname, left(rolpassword,14) FROM pg_authid; - Compare with the method on the matching
pg_hba.confline. If a legacy client cannot do SCRAM, the main fix is to upgrade its library or driver. - Only as a temporary measure, and only for a user whose stored hash is md5, put a per-user
md5line before the generalscram-sha-256line. If the stored password is SCRAM, themd5method switches to SCRAM and a legacy client still fails.host all mike .example.com md5 host all all .example.com scram-sha-256 - 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 thepg_hba.confmethod toscram-sha-256. - 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.
export PGSSLMODE=require
6.Fix "database does not exist" or "role does not exist"
Often the name is simply wrong, or the client defaulted the database name to the user name.
- Connect to the default
postgresdatabase, which usually exists, and list databases.psql -llists databases only, not roles. Without-d, the database name defaults to the user name, so a plainpsql -lcan itself fail withdatabase does not exist.psql -d postgres -l - Inside psql, list roles.
\du - Pass the database explicitly with
-d, or create it if it should exist.CREATE DATABASE x;
7.Fix "sorry, too many clients already"
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.
- See how connections are distributed by state (you may need a superuser or reserved connection to get in).
SELECT count(*), state FROM pg_stat_activity GROUP BY 2; - Fix applications that leak connections, and put a pooler such as PgBouncer in front of the database.
- Only if needed, raise
max_connectionsinpostgresql.confand restart. Note thatsuperuser_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-nullerrorvalues.- The application connects and the server log shows no new
FATALlines 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;andSHOW hba_file;, and that you restarted (forlisten_addresses,max_connections) or reloaded (forpg_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.confor 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
- Authentication Problems (PostgreSQL 18 docs, 20.16)Official · PostgreSQL Global Development Group
- Connections and Authentication (19.3)Official · PostgreSQL Global Development Group
- The pg_hba.conf File (20.1)Official · PostgreSQL Global Development Group
- Password Authentication (20.5)Official · PostgreSQL Global Development Group
- PostgreSQL Error Codes (Appendix A)Official · PostgreSQL Global Development Group
- Password authentication failed for user (Azure Database for PostgreSQL flexible server)Official · Microsoft Learn