简体   繁体   English

连接到远程Postgresql数据库时的连接超时

[英]connection timeout when connecting to a remote postgresql database

I am facing this problem: I have a program that loops the following: 我正面临这个问题:我有一个循环以下程序:

  • connect to a postgresql database 连接到PostgreSQL数据库
  • print a count number 打印计数
  • close the connection 关闭连接

code is as follow: 代码如下:

int i = 0;
    while(true){

        IConnection conn = ((ConnectionHelper)HelperFactory.getInstance().getHelper("ConnectionHelper")).getConnection("psql");

        if(conn != null && conn.connect()){
            conn.close();
            System.out.println(i++);
        }
    }

I connect to psql db by jdbc, something like this: 我通过jdbc连接到psql db,如下所示:

DriverManager.getConnection("jdbc:postgresql://" + host + ":5432/" + database, 
                                            user, password);

the conn.connect() returns true if it can successfully connect to the database (tested, I can retrieve data from it) 如果conn.connect()可以成功连接到数据库,则返回true(经过测试,我可以从中检索数据)

But i get the following result: 但我得到以下结果:

0 1 2 . 0 1 2。 . . 3919 3920 3921 3922 3919 3920 3921 3922

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.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:22)
    at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:30)
    at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:24)
    at org.postgresql.Driver.makeConnection(Driver.java:393)
    at org.postgresql.Driver.connect(Driver.java:267)

at the last loop the program stuck at trying to connect to psql til timeout. 在最后一个循环中,程序陷入尝试连接到psql直到超时的问题。 Any help? 有什么帮助吗?

Related to the exception message host or/and port where postgres is running not equals to the host or/and port what you put to the connect address, check it and you will find problem. 与运行postgres的异常消息主机或端口相关的信息不等于您放入连接地址的主机或端口所对应的内容,对其进行检查,您将发现问题。


And yes, your server cannot be reachable, some firewals and so on, check it too. 是的,您的服务器无法访问,有些防火墙等等,也请对其进行检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM