org.postgresql.util.PSQLException:连接被拒绝

时间:2013-05-22 18:44:15

标签: java linux postgresql jdbc driver

我想测试来自其他主机的索引功能的执行,因为这个功能在hadoop程序中没有完全正确地实现。但是我遇到了从其他主机连接到数据库的问题,如下所述。我不明白什么是问题。

我写了以下java程序

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();
            }
        }
}

我通过命令

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

因此我收到了以下消息

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

我从地址为192.168.1.10的主机启动了该文件。帮助消除错误。在什么原因导致错误?有什么需要做的?

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?

2 个答案:

答案 0 :(得分:7)

错误“检查主机名和端口是否正确以及postmaster是否接受TCP / IP连接。”表明:

  1. Postgres未启动,检查此次运行netstat -a并检查Postgres是否在等待5432端口上的连接

  2. 用户名/密码错误 - 尝试使用控制台或PG管理员使用这些凭据连接到Postgres

  3. 可能会发生,特别是如果Postgres在Linux机器上,由于安全原因,它不允许TCP / IP连接。你需要启用它。在网上搜索“启用远程Postgres连接”或“启用Postgres tcp / ip连接”,有大量的信息。

答案 1 :(得分:0)

您需要在postgres目录中配置postgresql.conf和pg_hba.conf文件。

打开 postgresql.conf 文件并替换行

listen_addresses = 'localhost'

使用

listen_addresses = '*'

如果#的jsut删除以“ listen_addresses”开头,则会使它们成为注释行。现在重新启动postgresql服务器。

pg_hba.conf

host    all             all              0.0.0.0/0                       md5

将此行添加到pg_hba.conf的末尾。 md5表示密码是连接所必需的,如果您不希望将任何密码更改为trust,则表示无条件接受连接。 0.0.0.0/0表示您还可以根据需要更改每个IP地址。