How to fix Server rejected establishment of SQL connection in PostgreSQL

PostgreSQLINTERMEDIATEMEDIUMUpdated Dec 26, 2025ยท 249 views

PostgreSQL rejects connection attempts when authentication fails, pg_hba.conf denies the client, or the server misconfiguration prevents connections. This typically involves credential issues, network configuration, or permission problems.

Quick fix

Fixes Server rejected establishment of SQL connection

lsof -i :5432
# or
ss -tlnp | grep 5432
  1. Verify the PostgreSQL server is running and accepting connections
  2. Verify your credentials are correct
  3. Check pg_hba.conf and ensure your host is authorized
  4. Ensure the authentication method matches your client setup
postgresql โ€” error
lsof -i :5432
# or
ss -tlnp | grep 5432
Server rejected establishment of SQL connection

What this error means

This error occurs when the PostgreSQL server explicitly rejects an incoming connection request. Unlike connection timeouts, this means the server is running and reachable but actively denies the attempt. The rejection happens at the authentication phase, after the initial TCP connection succeeds. Common reasons include incorrect credentials, the client IP not being authorized in pg_hba.conf, authentication method mismatches, or exceeding connection limits.

Typical symptoms

  • psql commands fail with "Server rejected establishment of SQL connection"
  • Applications cannot connect to the database even when credentials appear correct
  • Connection works locally but fails from remote clients
  • Authentication fails inconsistently or only for certain users
  • Server log shows "no pg_hba.conf entry for host..." messages

Common causes

  1. Incorrect username or password provided in connection string
  2. User role does not exist or lacks CONNECT privilege on the database
  3. pg_hba.conf does not contain an entry allowing the client IP address
  4. pg_hba.conf requires SSL/TLS but the client is not using encrypted connection
  5. Authentication method in pg_hba.conf (ident, peer, SCRAM) does not match client setup
  6. PostgreSQL server reached its max_connections limit
  7. Client uses wrong authentication type (e.g., password when ident is required)

How to fix "Server rejected establishment of SQL connection"

1Verify the PostgreSQL server is running and accepting connections

Check if PostgreSQL is listening on the expected port:

bash
lsof -i :5432
# or
ss -tlnp | grep 5432

If nothing listens on port 5432, start the PostgreSQL service:

bash
sudo systemctl start postgresql
# or on macOS
brew services start postgresql

Ensure the server is accepting TCP/IP connections by checking postgresql.conf:

2Verify your credentials are correct

Test the connection with known-good credentials. If you are unsure about the password, reset it:

bash
sudo -u postgres psql
# Inside psql:
ALTER USER postgres WITH PASSWORD 'newpassword';
\q

Then try connecting with the new password:

bash
psql -U postgres -h localhost -d postgres -W
# Enter password when prompted

If the user does not exist, create it:

bash
psql -U postgres -c "CREATE USER myuser WITH PASSWORD 'mypassword';"
3Check pg_hba.conf and ensure your host is authorized

Locate pg_hba.conf (usually in /etc/postgresql/VERSION/main/ on Linux or /usr/local/var/postgres on macOS):

bash
sudo -u postgres psql -c "SHOW hba_file;"

Open the file and verify it contains an entry for your client IP. For local connections, you typically need:

bash
local   all             all                                     trust
host    all             all             127.0.0.1/32            md5
host    all             all             ::1/128                 md5

For remote connections, add a line like:

bash
host    mydatabase      myuser          192.168.1.100/32        md5

After editing, reload the configuration:

bash
sudo systemctl reload postgresql
4Ensure the authentication method matches your client setup

The authentication method in pg_hba.conf must match how you are connecting.

For password authentication (most common), use md5 or scram-sha-256:

bash
host    mydb            myuser          192.168.1.0/24          scram-sha-256

For local connections using Unix sockets, use peer (requires matching OS user):

bash
local   all             myuser                                  peer

For SSL/TLS encrypted connections, ensure your pg_hba.conf entry starts with hostssl instead of host:

bash
hostssl    all             all             0.0.0.0/0              scram-sha-256

After changes, reload:

bash
sudo systemctl reload postgresql
5Check connection parameters and firewall rules

Verify that your connection string uses the correct host, port, and database name:

bash
psql -U myuser -h myhost.com -p 5432 -d mydatabase

For remote connections, ensure the firewall allows traffic on port 5432:

bash
# On the PostgreSQL server
sudo ufw allow 5432/tcp
# or
sudo firewall-cmd --add-port=5432/tcp --permanent
sudo firewall-cmd --reload

Test connectivity from the client:

bash
telnet myhost.com 5432
# or
nc -zv myhost.com 5432
6Check PostgreSQL server logs for detailed error messages

The server logs often contain more specific information about why a connection was rejected. Check the log file (usually /var/log/postgresql/postgresql.log on Linux):

bash
sudo tail -50 /var/log/postgresql/postgresql.log

Or use psql to view the last few messages:

bash
sudo -u postgres psql -c "SELECT * FROM pg_read_file('log/logfile', 0, 100000);"

Look for messages like "no pg_hba.conf entry" or "authentication method mismatch". These directly indicate the issue.

Advanced notes

Connection rejection can also occur if listen_addresses in postgresql.conf is not set correctly. By default, PostgreSQL listens only on localhost. To accept remote connections, set listen_addresses = '*' in postgresql.conf. Some systems use peer authentication for local connections, which requires the OS user to match the database user. For rootless environments or containers, you may need to explicitly set the authentication method to md5 or scram-sha-256. Always check the server logs (in postgresql.log or via the log_connections setting) for the most accurate error description.

Frequently asked questions

What does "Server rejected establishment of SQL connection" mean?
This error occurs when the PostgreSQL server explicitly rejects an incoming connection request. Unlike connection timeouts, this means the server is running and reachable but actively denies the attempt. The rejection happens at the authentication phase, after the initial TCP connection succeeds.
How do I fix "Server rejected establishment of SQL connection"?
PostgreSQL rejects connection attempts when authentication fails, pg_hba.conf denies the client, or the server misconfiguration prevents connections. This typically involves credential issues, network configuration, or permission problems.
What causes "Server rejected establishment of SQL connection"?
Incorrect username or password provided in connection string User role does not exist or lacks CONNECT privilege on the database pg_hba.conf does not contain an entry allowing the client IP address
Is the "Server rejected establishment of SQL connection" error serious?
It is a medium-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading