Error
Error Code:
08004
PostgreSQL Error 08004: Connection Establishment Rejected
Description
This error signifies that the client application was unable to establish a connection to the PostgreSQL server. It commonly arises when the database server or an intermediary network component denies the connection request, preventing any communication.
Error Message
sqlserver rejected establishment of sqlconnection
Known Causes
4 known causesIncorrect Connection Details
The hostname, port, database name, username, or password specified in the client's connection string is inaccurate.
PostgreSQL Server Unreachable
The PostgreSQL server might not be running, or a firewall is actively blocking incoming connection attempts from the client.
Authentication Failure
The server rejected the connection due to invalid credentials (username/password) or a mismatch with the authentication methods defined in pg_hba.conf.
Maximum Connections Exceeded
The PostgreSQL server has reached its configured limit for concurrent client connections, preventing any new connection attempts.
Solutions
4 solutions available1. Verify PostgreSQL Server Status easy
Ensure the PostgreSQL server process is running and accessible.
1
Check if the PostgreSQL service is running on the server. The command to do this varies by operating system.
# For systems using systemd (e.g., Ubuntu 15.04+, Debian 8+, CentOS 7+)
sudo systemctl status postgresql
# For systems using init.d (e.g., older Ubuntu/Debian, CentOS 6)
sudo service postgresql status
# For Windows (using Services manager, search for PostgreSQL)
2
If the service is not running, start it.
# For systems using systemd
sudo systemctl start postgresql
# For systems using init.d
sudo service postgresql start
# For Windows (using Services manager, right-click and select 'Start')
3
Attempt to connect again after ensuring the server is running.
psql -h your_host -p your_port -U your_user -d your_database
2. Check Network Connectivity and Firewall Rules medium
Confirm that the client can reach the PostgreSQL server over the network and that no firewalls are blocking the connection.
1
From the client machine, attempt to ping the PostgreSQL server's IP address or hostname to verify basic network reachability.
ping your_postgres_server_ip_or_hostname
2
Use `telnet` or `nc` (netcat) to test if the PostgreSQL port (default is 5432) is open and accepting connections from the client.
# Using telnet
telnet your_postgres_server_ip_or_hostname 5432
# Using nc (netcat)
nc -vz your_postgres_server_ip_or_hostname 5432
3
If the `telnet` or `nc` command fails or times out, investigate firewall rules on both the client and server. Ensure that the PostgreSQL port (e.g., 5432) is allowed for inbound connections on the server and outbound connections on the client.
# Example for UFW (Ubuntu/Debian)
sudo ufw status
sudo ufw allow 5432/tcp
# Example for firewalld (CentOS/RHEL/Fedora)
sudo firewall-cmd --list-all
sudo firewall-cmd --permanent --add-port=5432/tcp
sudo firewall-cmd --reload
# On Windows, check Windows Defender Firewall or any third-party firewall.
3. Review PostgreSQL `pg_hba.conf` Configuration medium
Verify that the `pg_hba.conf` file permits connections from the client's IP address and with the specified user and database.
1
Locate your `pg_hba.conf` file. The location varies depending on your PostgreSQL installation and operating system. Common locations include `/etc/postgresql/<version>/main/pg_hba.conf` or within the data directory.
# You can find the data directory using:
SELECT setting FROM pg_settings WHERE name = 'data_directory';
2
Edit the `pg_hba.conf` file. Add or modify a line to allow connections from your client. The format is: `TYPE DATABASE USER ADDRESS METHOD`
# Example: Allow user 'myuser' to connect to 'mydatabase' from any IP address on the local network (192.168.1.0/24)
host mydatabase myuser 192.168.1.0/24 md5
# Example: Allow all users to connect to all databases from a specific IP address
host all all 192.168.1.100/32 md5
# Example: Allow all users to connect to all databases from any IP address (use with caution!)
host all all 0.0.0.0/0 md5
3
After modifying `pg_hba.conf`, you must reload the PostgreSQL configuration for the changes to take effect. This does not require a server restart.
sudo systemctl reload postgresql # For systems using systemd
# OR
sudo service postgresql reload # For systems using init.d
# Alternatively, connect as a superuser and run:
SELECT pg_reload_conf();
4. Check PostgreSQL Listening Addresses medium
Ensure PostgreSQL is configured to listen on the network interface from which the client is attempting to connect.
1
Locate your `postgresql.conf` file. Similar to `pg_hba.conf`, its location varies. Common paths include `/etc/postgresql/<version>/main/postgresql.conf`.
# You can find the data directory using:
SELECT setting FROM pg_settings WHERE name = 'data_directory';
2
Edit `postgresql.conf` and find the `listen_addresses` parameter. By default, it might be set to `localhost`, meaning it only accepts connections from the server itself.
# To listen on all available network interfaces (use with caution, especially if you have sensitive data):
listen_addresses = '*'
# To listen on a specific IP address:
listen_addresses = '192.168.1.50'
# To listen on localhost and a specific IP address:
listen_addresses = 'localhost, 192.168.1.50'
3
If you changed `listen_addresses`, you need to restart the PostgreSQL service for the changes to take effect.
sudo systemctl restart postgresql # For systems using systemd
# OR
sudo service postgresql restart # For systems using init.d