Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.postgresql.util.PSQLException: Connection refused

I wanted to test execution of function of an indexing from other host because this function not absolutely correctly fulfilled in hadoop to the program. But I had problems with connection to a database from other host as it is explained below. I don't understand in what a problem.

I wrote following the java program

import java.io.*;
import java.util.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.*;

public class Sample {
    public static void main (String[] args) throws IOException  {
                    int CountComputers;
            FileInputStream fstream = new FileInputStream(
                    "/export/hadoop-1.0.1/bin/countcomputers.txt");
            BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
            String result=br.readLine();
            CountComputers=Integer.parseInt(result);
            input.close();
            fstream.close();
            Connection con = null;
            Statement st = null;
                ResultSet rs = null;    
               String url = "jdbc:postgresql://192.168.1.8:5432/NexentaSearch";
                String user = "postgres";
                String password = "valter89";
            //Class.forName("org.postgresql.Driver");
            ArrayList<String> paths = new ArrayList<String>();
            try
            {
                Class.forName("org.postgresql.Driver");
                con = DriverManager.getConnection(url, user, password);
                        st = con.createStatement();
                        rs = st.executeQuery("select path from tasks order by id");
                while (rs.next()) { paths.add(rs.getString(1)); };
                PrintWriter zzz = null;
                    try
                    {
                            zzz = new PrintWriter(new FileOutputStream("/export/hadoop-1.0.1/bin/readwaysfromdatabase.txt"));
                    }
                    catch(FileNotFoundException e)
                    {
                            System.out.println("Error");
                            System.exit(0);
                    }
                    for (int i=0; i<paths.size(); i++)
                {
                    zzz.println("paths[i]=" + paths.get(i) + "\n");
                    }
                    zzz.close();

            }
            catch (SQLException e) 
            {
                System.out.println("Connection Failed! Check output console");
                e.printStackTrace();
            }
            catch (ClassNotFoundException e)
            {
                e.printStackTrace();
            }
        }
}

I launched Sample.jar by means of a command

./java -jar -Djava.library.path=/opt/jdk1.7.0_06/lib /Samplejavaprogram/Sample.jar

and as a result I received the following messages

Connection Failed! Check output console
org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:136)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:66)
    at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:125)
    at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:30)
    at org.postgresql.jdbc3.Jdbc3Connection.<init>(Jdbc3Connection.java:24)
    at org.postgresql.Driver.makeConnection(Driver.java:393)
    at org.postgresql.Driver.connect(Driver.java:267)
    at java.sql.DriverManager.getConnection(DriverManager.java:579)
    at java.sql.DriverManager.getConnection(DriverManager.java:221)
    at Sample.main(Sample.java:33)
Caused by: java.net.ConnectException: Connection timed out
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
    at java.net.Socket.connect(Socket.java:579)
    at java.net.Socket.connect(Socket.java:528)
    at java.net.Socket.<init>(Socket.java:425)
    at java.net.Socket.<init>(Socket.java:208)
    at org.postgresql.core.PGStream.<init>(PGStream.java:62)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:76)
    ... 9 more

I launched the file from a host with the address 192.168.1.10. Help to eliminate an error. In what cause of error? What it is necessary to make?

netstat -a


UDP: IPv4
   Local Address        Remote Address      State
-------------------- -------------------- ----------
      *.bootpc                            Idle
      *.dhcpv6-client                      Idle
      *.*                                 Unbound
      *.*                                 Unbound
      *.sunrpc                            Idle
      *.*                                 Unbound
      *.38606                             Idle
      *.sunrpc                            Idle
      *.*                                 Unbound
      *.40712                             Idle
      *.*                                 Unbound
      *.*                                 Unbound
      *.*                                 Unbound
      *.54122                             Idle
      *.41742                             Idle
      *.*                                 Unbound
      *.lockd                             Idle
      *.lockd                             Idle
      *.59670                             Idle
      *.42840                             Idle
      *.43065                             Idle
      *.39546                             Idle
      *.34243                             Idle
      *.35383                             Idle
      *.40746                             Idle
      *.40832                             Idle
192.168.1.2.bootpc                        Idle
      *.mdns                              Idle
      *.mdns                              Idle
192.168.1.2.ntp                           Idle
      *.snmpd                             Idle
      *.ntp                               Idle
      *.ntp                               Idle
localhost.localdomain.ntp                      Idle
myhost2.ntp                               Idle

UDP: IPv6
   Local Address                     Remote Address                   State      If
--------------------------------- --------------------------------- ---------- -----
      *.dhcpv6-client                                               Idle       
      *.*                                                           Unbound    
localhost.44111                   localhost.44111                   Connected  
      *.sunrpc                                                      Idle       
      *.*                                                           Unbound    
      *.38606                                                       Idle       
      *.54122                                                       Idle       
      *.*                                                           Unbound    
      *.lockd                                                       Idle       
      *.42840                                                       Idle       
      *.39546                                                       Idle       
      *.35383                                                       Idle       
      *.40832                                                       Idle       
      *.ntp                                                         Idle       
localhost.ntp                                                       Idle       

TCP: IPv4
   Local Address        Remote Address    Swind Send-Q Rwind Recv-Q    State
-------------------- -------------------- ----- ------ ----- ------ -----------
localhost.localdomain.4999       *.*                0      0 128000      0 LISTEN
      *.2001               *.*                0      0 128000      0 LISTEN
      *.2002               *.*                0      0 128000      0 LISTEN
      *.2003               *.*                0      0 128000      0 LISTEN
      *.16049              *.*                0      0 128000      0 LISTEN
      *.5432               *.*                0      0 128000      0 LISTEN
      *.sunrpc             *.*                0      0 128000      0 LISTEN
      *.*                  *.*                0      0 128000      0 IDLE
      *.sunrpc             *.*                0      0 128000      0 LISTEN
      *.*                  *.*                0      0 128000      0 IDLE
      *.54002              *.*                0      0 128000      0 LISTEN
      *.49023              *.*                0      0 128000      0 LISTEN
      *.55493              *.*                0      0 1049200      0 LISTEN
      *.46659              *.*                0      0 1048952      0 LISTEN
      *.lockd              *.*                0      0 1049200      0 LISTEN
      *.lockd              *.*                0      0 1048952      0 LISTEN
      *.45361              *.*                0      0 128000      0 LISTEN
localhost.localdomain.36084 localhost.localdomain.2001 65440      0 139264      0 ESTABLISHED
      *.53943              *.*                0      0 128000      0 LISTEN
localhost.localdomain.2001 localhost.localdomain.36084 139060      0 130880      0 ESTABLISHED
localhost.localdomain.44644 localhost.localdomain.2012 34765      0 139264      0 ESTABLISHED
localhost.localdomain.2012 localhost.localdomain.44644 139060      0 139060      0 ESTABLISHED
      *.36068              *.*                0      0 128000      0 LISTEN
      *.57055              *.*                0      0 128000      0 LISTEN
      *.login              *.*                0      0 128000      0 LISTEN
      *.finger             *.*                0      0 128000      0 LISTEN
      *.telnet             *.*                0      0 128000      0 LISTEN
      *.shell              *.*                0      0 128000      0 LISTEN
      *.40524              *.*                0      0 128000      0 LISTEN
      *.60838              *.*                0      0 128000      0 LISTEN
      *.47648              *.*                0      0 128000      0 LISTEN
      *.56894              *.*                0      0 128000      0 LISTEN
      *.48461              *.*                0      0 128000      0 LISTEN
      *.53672              *.*                0      0 128000      0 LISTEN
      *.42108              *.*                0      0 128000      0 LISTEN
      *.63720              *.*                0      0 128000      0 LISTEN
      *.ssh                *.*                0      0 128000      0 LISTEN
localhost.localdomain.smtp       *.*                0      0 128000      0 LISTEN
localhost.localdomain.submission       *.*                0      0 128000      0 LISTEN
      *.80                 *.*                0      0 128000      0 LISTEN
      *.*                  *.*                0      0 128000      0 IDLE
      *.2000               *.*                0      0 128000      0 LISTEN
      *.*                  *.*                0      0 128000      0 IDLE
      *.443                *.*                0      0 128000      0 LISTEN
      *.*                  *.*                0      0 128000      0 IDLE
localhost.localdomain.38137 localhost.localdomain.2003 65440      0 139264      0 ESTABLISHED
localhost.localdomain.2003 localhost.localdomain.38137 139060      0 130880      0 ESTABLISHED
localhost.localdomain.63918 localhost.localdomain.2001 65440      0 139264      0 ESTABLISHED
localhost.localdomain.2001 localhost.localdomain.63918 139060      0 130880      0 ESTABLISHED
myhost2.ssh          192.168.1.4.7431     65548      0 128480      0 ESTABLISHED
localhost.localdomain.34057 localhost.localdomain.2001 65440      0 139264      0 ESTABLISHED
localhost.localdomain.2001 localhost.localdomain.34057 139060      0 130880      0 ESTABLISHED
localhost.localdomain.37015 localhost.localdomain.2001 65440      0 139264      0 ESTABLISHED
localhost.localdomain.2001 localhost.localdomain.37015 139060      0 130880      0 ESTABLISHED
      *.2011               *.*                0      0 131072      0 LISTEN
localhost.localdomain.36598 localhost.localdomain.2002 65440      0 139264      0 ESTABLISHED
localhost.localdomain.2002 localhost.localdomain.36598 139060      0 130880      0 ESTABLISHED
localhost.localdomain.4000       *.*                0      0 131072      0 LISTEN
localhost.localdomain.53161 localhost.localdomain.2011 34765      0 139264      0 ESTABLISHED
localhost.localdomain.2011 localhost.localdomain.53161 139060      0 139060      0 ESTABLISHED
localhost.localdomain.43878 localhost.localdomain.2001 65440      0 139264      0 ESTABLISHED
localhost.localdomain.2001 localhost.localdomain.43878 139060      0 130880      0 ESTABLISHED
localhost.localdomain.50976 localhost.localdomain.2001 65440      0 139264      0 ESTABLISHED
localhost.localdomain.2001 localhost.localdomain.50976 139060      0 130880      0 ESTABLISHED
localhost.localdomain.62633 localhost.localdomain.2012 34765      0 139264      0 ESTABLISHED
localhost.localdomain.2012 localhost.localdomain.62633 139060      0 139060      0 ESTABLISHED
      *.2012               *.*                0      0 131072      0 LISTEN
localhost.localdomain.34418 localhost.localdomain.2012 34765      0 139264      0 ESTABLISHED
localhost.localdomain.2012 localhost.localdomain.34418 139060      0 139060      0 ESTABLISHED
localhost.localdomain.34356 localhost.localdomain.2011 34765      0 139264      0 ESTABLISHED
localhost.localdomain.2011 localhost.localdomain.34356 139060      0 139060      0 ESTABLISHED
localhost.localdomain.62884 localhost.localdomain.2001 65440      0 139264      0 ESTABLISHED
localhost.localdomain.2001 localhost.localdomain.62884 139060      0 130880      0 ESTABLISHED
localhost.localdomain.52603 localhost.localdomain.2011 34765      0 139264      0 ESTABLISHED
localhost.localdomain.2011 localhost.localdomain.52603 139060      0 139060      0 ESTABLISHED
localhost.localdomain.35345 localhost.localdomain.2001 65440      0 139264      0 ESTABLISHED
localhost.localdomain.2001 localhost.localdomain.35345 139060      0 130880      0 ESTABLISHED
localhost.localdomain.49022 localhost.localdomain.2001 130880      0 139264      0 TIME_WAIT
localhost.localdomain.42797 localhost.localdomain.2001 130880      0 139264      0 TIME_WAIT
localhost.localdomain.46312 localhost.localdomain.2001 130880      0 139264      0 TIME_WAIT

TCP: IPv6
   Local Address                     Remote Address                 Swind Send-Q Rwind Recv-Q   State      If
--------------------------------- --------------------------------- ----- ------ ----- ------ ----------- -----
      *.5432                            *.*                             0      0 128000      0 LISTEN      
      *.sunrpc                          *.*                             0      0 128000      0 LISTEN      
      *.*                               *.*                             0      0 128000      0 IDLE        
      *.54002                           *.*                             0      0 128000      0 LISTEN      
      *.55493                           *.*                             0      0 1049200      0 LISTEN      
      *.lockd                           *.*                             0      0 1049200      0 LISTEN      
      *.53943                           *.*                             0      0 128000      0 LISTEN      
      *.57055                           *.*                             0      0 128000      0 LISTEN      
      *.login                           *.*                             0      0 128000      0 LISTEN      
      *.finger                          *.*                             0      0 128000      0 LISTEN      
      *.telnet                          *.*                             0      0 128000      0 LISTEN      
      *.shell                           *.*                             0      0 128000      0 LISTEN      
      *.60838                           *.*                             0      0 128000      0 LISTEN      
      *.56894                           *.*                             0      0 128000      0 LISTEN      
      *.53672                           *.*                             0      0 128000      0 LISTEN      
      *.63720                           *.*                             0      0 128000      0 LISTEN      
      *.ssh                             *.*                             0      0 128000      0 LISTEN      
localhost.smtp                          *.*                             0      0 128000      0 LISTEN      
      *.80                              *.*                             0      0 128000      0 LISTEN      
      *.2000                            *.*                             0      0 128000      0 LISTEN      
      *.443                             *.*                             0      0 128000      0 LISTEN      

Active UNIX domain sockets
Address  Type          Vnode     Conn  Local Addr      Remote Addr
ffffff0119f107b8 stream-ord 00000000     00000000                                  
ffffff0119f10b68 stream-ord 00000000     00000000                                  
ffffff0119214050 stream-ord 00000000     00000000                                  
ffffff0119214400 stream-ord 00000000     00000000                                  
ffffff0119214b60 stream-ord 00000000     00000000                                  
ffffff0114119048 stream-ord 00000000     00000000                                  
ffffff01141193f8 stream-ord ffffff011950e580 00000000    /tmp/dbus-3YiNfXlgwn                
ffffff01141197a8 stream-ord ffffff0119217e40 00000000    /tmp/dbus-7BNzAEnmYY                
ffffff0114119b58 stream-ord ffffff0114118e40 00000000    /var/run/mDNSResponder                
ffffff01138b2040 stream-ord 00000000     00000000    /var/run/dbus/system_bus_socket                
ffffff01138b23f0 stream-ord 00000000     ffffff0111d0e780                /var/run/dbus/system_bus_socket 
ffffff01138b27a0 stream-ord 00000000     00000000    /var/run/dbus/system_bus_socket                
ffffff01138b2b50 stream-ord 00000000     ffffff0111d0e780                /var/run/dbus/system_bus_socket 
ffffff0113059038 stream-ord ffffff0113835140 00000000    /var/run/.inetd.uds                
ffffff01130593e8 stream-ord 00000000     ffffff0112c5f880                /var/run/hald/dbus-9g2E5zdff9 
ffffff0113059798 stream-ord 00000000     00000000    /var/run/hald/dbus-9g2E5zdff9                
ffffff0113059b48 dgram      ffffff01130b2e00 00000000    /var/run/in.ndpd_mib                
ffffff011306d030 stream-ord ffffff0112bb5280 00000000    /var/run/in.ndpd_ipadm                
ffffff011306d3e0 stream-ord 00000000     00000000    /var/run/hald/dbus-9g2E5zdff9                
ffffff011306d790 stream-ord 00000000     00000000    /var/run/hald/dbus-9g2E5zdff9                
ffffff011306db40 stream-ord 00000000     ffffff0112c5f880                /var/run/hald/dbus-9g2E5zdff9 
ffffff0112fcb028 stream-ord 00000000     ffffff0112c5f880                /var/run/hald/dbus-9g2E5zdff9 
ffffff0112fcb3d8 stream-ord 00000000     00000000    /var/run/hald/dbus-9g2E5zdff9                
ffffff0112fcb788 stream-ord 00000000     ffffff0112c5f880                /var/run/hald/dbus-9g2E5zdff9 
ffffff0112fcbb38 stream-ord ffffff0112fd4080 00000000    /tmp/.s.PGSQL.5432                
ffffff01122c4020 stream-ord 00000000     00000000    /var/run/hald/dbus-F83hT9CGSX                
ffffff01122c43d0 stream-ord 00000000     ffffff0112c5f280                /var/run/hald/dbus-F83hT9CGSX 
ffffff01122c4780 stream-ord ffffff0112c5f280 00000000    /var/run/hald/dbus-F83hT9CGSX                
ffffff01122c4b30 stream-ord 00000000     00000000                                  
ffffff0112294018 stream-ord 00000000     00000000                                  
ffffff01122943c8 stream-ord ffffff0112c5f880 00000000    /var/run/hald/dbus-9g2E5zdff9                
ffffff0112294778 stream-ord 00000000     00000000                                  
ffffff0112294b28 stream-ord 00000000     00000000                                  
ffffff0112278010 stream-ord ffffff01122c7880 00000000    /tmp/dbus-PuCCfO9I4C                
ffffff01122783c0 stream-ord 00000000     00000000                                  
ffffff0112278770 stream-ord 00000000     00000000                                  
ffffff0112278b20 stream-ord ffffff01122a0b40 00000000    /tmp/dbus-tfcmjjMClq                
ffffff0111d08008 stream-ord 00000000     00000000                                  
ffffff0111d083b8 stream-ord 00000000     00000000                                  
ffffff0111d08768 stream-ord ffffff01121a2b40 00000000    /tmp/dbus-cEAGQX2WDo                
ffffff0111d08b18 stream-ord ffffff0111d0e780 00000000    /var/run/dbus/system_bus_socket                

Postgresql is launched?

like image 547
user2328488 Avatar asked May 22 '13 18:05

user2328488


2 Answers

Error "Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections." suggests:

  1. Postgres is not started, to check this run netstat -a and check if Postgres is waiting for connections on 5432 port

  2. wrong username/password - try to connect to Postgres using console or PG Admin using these credentials

  3. it may happen, especially if Postgres is on Linux box, that is does not allow TCP/IP connections because of the security reasons. You need to enable that. Search the net for "enable remote Postgres connection" or "enable Postgres tcp/ip connection", there is planty of information.

like image 110
Piotr Kochański Avatar answered Nov 03 '22 22:11

Piotr Kochański


You need to configure postgresql.conf and pg_hba.conf file in your postgres directory.

Open postgresql.conf file and replace line

listen_addresses = 'localhost'

with

listen_addresses = '*'

if there is a '#' start of listen_addresses just delete it, it makes them comment line. now restart postgresql server.

pg_hba.conf

host    all             all              0.0.0.0/0                       md5

add this line at the end of the pg_hba.conf. md5 means password is neccesary for connection if you dont want any password you can change it to trust this accepts connection unconditionally. 0.0.0.0/0 means every ip address, you can also change it as you want.

like image 26
Arty Avatar answered Nov 03 '22 22:11

Arty