1

我的 PC 上有一个 PostgreSQL 数据库。Postgres 服务器在端口 5432 上运行。当我在另一台 PC 上输入 时telnet server.ip 5432,我设法连接。但是,当我尝试使用 Java 连接时:

connection = DriverManager.getConnection("Jdbc:postgresql:mydb://server.ip:5432/", "user", "pass");

我收到以下错误:Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.有人可以告诉我可能是什么问题吗?

更新:

我将连接字符串更改为:

connection = DriverManager.getConnection("jdbc:postgresql://server.ip:5432/mydb", "user", "pass");

现在我得到了错误FATAL: no pg_hba.conf entry for host "client.ip", user "user", database "mydb", SSL off

4

2 回答 2

2

您的 JDBC 连接字符串错误。文档说这些是允许的格式:

jdbc:postgresql:database
jdbc:postgresql://host/database
jdbc:postgresql://host:port/database

不是- 就像你的情况一样:

Jdbc:postgresql:databse://host:port/

(另请注意字符串中的大写字母J也是不允许的)

于 2013-04-28T18:06:29.013 回答
1

您需要配置身份验证文件/etc/postgresql/x.x/main/pg_hba.conf

添加这些行:

# TYPE DATABASE  USER  ADDRESS                   METHOD
host   all       all   my.pc.ip/Prefix-netmask   md5

之后重新启动 postgresql 服务器:

# /etc/init.d/postgresql restart
于 2013-04-28T18:03:07.680 回答