Error : org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections in java

when i try to create some connection in java with JDCB Postgresql , i have some problem . I cant create connection succesfully .

here the code :

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package Testapp;

/**
*
* @author penguintengil
*/
import java.sql.*;
import javax.swing.JOptionPane;
public class Login {
public Connection conn=null;
public final String driver=”org.postgresql.Driver”; //drivernya postgre
public final String user=”root”; //username
public final String pass=”root”; //password
public final String url=”jdbc:postgresql://localhost/test”; //url postgre

public void conn(){

try {
Class.forName(driver);
conn=DriverManager.getConnection(url,user,pass);
System.err.println(“Connection Success”);
} catch (Exception e) {
JOptionPane.showMessageDialog(null,”Connection Failed”);
System.err.println(“Error: “+e);
}

}
public static void main (String[]arg){
Login k = new Login();
k.conn();
}

Statement createStatement() {
throw new UnsupportedOperationException(“Not yet implemented”);
}
}

Compile > Run

if you got an error like

Error : org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections

Lets try to fix this

check configuration in /var/lib/pgsql/data/pg_hba.conf

put some

# TYPE DATABASE USER ADDRESS METHOD

# “local” is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the

local all all trust
host all all 127.0.0.1 255.255.255.255 trust
host all all ::1 ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff trust

save

and then check  /var/lib/pgsql/data/postgresql.conf

change

listen_addresses=’*’

restart service

#service postgresql restart

Finally , running program .

and connection will be succesfull 🙂

3 thoughts on “Error : org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections in java

  1. On windows with postgre 9.2 service not getting started for local domain and even after commenting local part and starting the service ,,My application is still giving the same error.

    Like

Leave a comment