How to fix Server rejected establishment of SQL connection in PostgreSQL
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- Verify the PostgreSQL server is running and accepting connections
- Verify your credentials are correct
- Check pg_hba.conf and ensure your host is authorized
- Ensure the authentication method matches your client setup
lsof -i :5432# orss -tlnp | grep 5432Server 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
- 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
- pg_hba.conf requires SSL/TLS but the client is not using encrypted connection
- Authentication method in pg_hba.conf (ident, peer, SCRAM) does not match client setup
- PostgreSQL server reached its max_connections limit
- Client uses wrong authentication type (e.g., password when ident is required)
How to fix "Server rejected establishment of SQL connection"
Check if PostgreSQL is listening on the expected port:
lsof -i :5432
# or
ss -tlnp | grep 5432If nothing listens on port 5432, start the PostgreSQL service:
sudo systemctl start postgresql
# or on macOS
brew services start postgresqlEnsure the server is accepting TCP/IP connections by checking postgresql.conf:
Test the connection with known-good credentials. If you are unsure about the password, reset it:
sudo -u postgres psql
# Inside psql:
ALTER USER postgres WITH PASSWORD 'newpassword';
\qThen try connecting with the new password:
psql -U postgres -h localhost -d postgres -W
# Enter password when promptedIf the user does not exist, create it:
psql -U postgres -c "CREATE USER myuser WITH PASSWORD 'mypassword';"Locate pg_hba.conf (usually in /etc/postgresql/VERSION/main/ on Linux or /usr/local/var/postgres on macOS):
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:
local all all trust
host all all 127.0.0.1/32 md5
host all all ::1/128 md5For remote connections, add a line like:
host mydatabase myuser 192.168.1.100/32 md5After editing, reload the configuration:
sudo systemctl reload postgresqlThe authentication method in pg_hba.conf must match how you are connecting.
For password authentication (most common), use md5 or scram-sha-256:
host mydb myuser 192.168.1.0/24 scram-sha-256For local connections using Unix sockets, use peer (requires matching OS user):
local all myuser peerFor SSL/TLS encrypted connections, ensure your pg_hba.conf entry starts with hostssl instead of host:
hostssl all all 0.0.0.0/0 scram-sha-256After changes, reload:
sudo systemctl reload postgresqlVerify that your connection string uses the correct host, port, and database name:
psql -U myuser -h myhost.com -p 5432 -d mydatabaseFor remote connections, ensure the firewall allows traffic on port 5432:
# On the PostgreSQL server
sudo ufw allow 5432/tcp
# or
sudo firewall-cmd --add-port=5432/tcp --permanent
sudo firewall-cmd --reloadTest connectivity from the client:
telnet myhost.com 5432
# or
nc -zv myhost.com 5432The 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):
sudo tail -50 /var/log/postgresql/postgresql.logOr use psql to view the last few messages:
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
- ๐PostgreSQL Official Documentation
- ๐PostgreSQL Docs - Authentication Problems(Official Docs)
- ๐PostgreSQL Docs - pg_hba.conf Configuration(Official Docs)