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.
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.
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.
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.
ERROR: syntax error at end of input
Syntax error at end of input in PostgreSQL
Bind message supplies N parameters but prepared statement requires M
Bind message supplies N parameters but prepared statement requires M in PostgreSQL
Multidimensional arrays must have sub-arrays with matching dimensions
Multidimensional arrays must have sub-arrays with matching dimensions
ERROR: value too long for type character varying
Value too long for type character varying
insufficient columns in unique constraint for partition key
How to fix "insufficient columns in unique constraint for partition key" in PostgreSQL