Java 连接到 <hostname>:5432 被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31268668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 10:51:46  来源:igfitidea点击:

Connection to <hostname>:5432 refused

javapostgresqljdbc

提问by Vekszor

I'm not a pro in communication networks, so I have no idea what should be my "hostname" and where can I find it?

我不是通信网络的专家,所以我不知道我的“主机名”应该是什么以及在哪里可以找到它?

I created a postgreSQL server on ubuntu 14.04, I can connect to it from the same computer without problem by setting hostname to 127.0.0.1(localhost), or even 192.168.1.42 (my private ip). But I can not connect to my server from any other computer, even if they are on local network or not. I always get this message:

我在 ubuntu 14.04 上创建了一个 postgreSQL 服务器,我可以通过将主机名设置为 127.0.0.1(localhost) 或什至 192.168.1.42(我的私有 ip)从同一台计算机连接到它。但是我无法从任何其他计算机连接到我的服务器,即使它们是否在本地网络上。我总是收到这条消息:

"Connection to <hostname>:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections."

where is either 127.0.1.1, 127.0.0.1, 192.168.1.42, 98.765.432.123(public ip) or anything else, it never works. What is the right hostname?

哪里是 127.0.1.1、127.0.0.1、192.168.1.42、98.765.432.123(public ip) 或其他任何东西,它永远不会工作。什么是正确的主机名?

I already set listen_addresses to '*', already edited pg_hba.conf to accept 0.0.0.0/0 IP-s, and disabled ubuntu firewall.

我已经将 listen_addresses 设置为 '*',已经编辑 pg_hba.conf 以接受 0.0.0.0/0 IP-s,并禁用了 ubuntu 防火墙。

采纳答案by Patrick

This could be caused by many things.

这可能是由很多事情引起的。

  1. The server has an IP address. That is the address that the JDBC connection string should use for <hostname>, unless you have some local DNS solution. To rule out problems with the latter, use the IP address of the Ubuntu server. On Ubuntu type ifconfigto see it; it probably is 192.168.1.42like you mentioned. So from the client with the Java app, you use the IP address of the server.
  2. If you have a standard PostgreSQL installation, it should use port 5432, you can check the setting in postgresql.conf. In the JDBC connection string you should also indicate the port: "jdbc:postgresql://192.168.1.42:5432/...", just to be on the safe side.
  3. You need to have a database that you can connect to. Note that by default only the owner of the database (probably the user postgresin your case) can connect to it. See further down.
  4. In pg_hba.confyou need to create an entry for your new database so it can be connected to (typical settings, check your network setup). The IP address (range) you specify here is that of the clients connecting to the database!

    host my_db all 192.168.0.0/16 md5

  1. 服务器有一个IP地址。这是 JDBC 连接字符串应该用于 的地址<hostname>,除非您有一些本地 DNS 解决方案。要排除后者的问题,请使用 Ubuntu 服务器的 IP 地址。在 Ubuntu 上键入ifconfig查看它;它可能192.168.1.42就像你提到的那样。因此,从带有 Java 应用程序的客户端,您使用服务器的 IP 地址。
  2. 如果你有一个标准的 PostgreSQL 安装,它应该使用端口 5432,你可以在postgresql.conf. 在 JDBC 连接字符串中,您还应该指明端口:“jdbc:postgresql://192.168.1.42:5432/...”,以防万一。
  3. 您需要有一个可以连接的数据库。请注意,默认情况下只有数据库的所有者(postgres在您的情况下可能是用户)可以连接到它。往下看。
  4. pg_hba.conf您需要为您的新数据库中创建一个条目,它可以连接到(典型的设置,请检查您的网络设置)。您在此处指定的 IP 地址(范围)是连接到数据库的客户端的 IP 地址!

    host my_db all 192.168.0.0/16 md5

You must restart your PostgreSQL server after modifying pg_hba.conf. (Typically on Ubuntu, do sudo ./etc/init.d/postgresql restart)

修改后必须重新启动 PostgreSQL 服务器pg_hba.conf。(通常在 Ubuntu 上,做sudo ./etc/init.d/postgresql restart

To create database my_db(or whatever name you prefer), go to your Ubuntu box and enter psqlas the postgresuser:

要创建数据库my_db(或您喜欢的任何名称),请转到您的 Ubuntu 框并psqlpostgres用户身份输入:

vekszor@ubuntu:~$ sudo -u postgres psql
[sudo] password for user:
psql (9.3.5)
Type "help" for help

postgres=# 

In order to connect to the database, you should have a database:

为了连接到数据库,你应该有一个数据库:

postgres=# CREATE DATABASE my_db;
CREATE DATABASE

You also want a user (role) that is less powerful than the postgressuperuser, but still able to manipulate the database. So create this user role and assign ownership of the database:

您还需要一个权限低于postgres超级用户但仍能够操作数据库的用户(角色)。所以创建这个用户角色并分配数据库的所有权:

postgres=# CREATE ROLE vekszor LOGIN PASSWORD 'secret123' CREATEROLE;
CREATE ROLE
postgres=# ALTER DATABASE my_db OWNER TO vekszor;
ALTER

Now you can go back to your client computer with the Java app and finish the JDBC connection string with the name of the database, the user and password.

现在,您可以使用 Java 应用程序返回到您的客户端计算机,并使用数据库名称、用户名和密码完成 JDBC 连接字符串。

Note that if you want to access your database from the internet, you should set up a NAT rule in your router to point traffic on port 5432 to the IP of your Ubuntu server.So long as the internal address of the router is in the same address range of the client computers that you indicated in pg_hba.confthis should be easy to set up, otherwise add a new entry in pg_hba.conf.

注意,如果你想从互联网访问你的数据库,你应该在你的路由器中设置一个 NAT 规则,将端口 5432 上的流量指向你的 Ubuntu 服务器的 IP。只要路由器的内部地址是相同的您在pg_hba.conf此指定的客户端计算机的地址范围应该易于设置,否则在pg_hba.conf.

回答by Danish Khakwani

Reading your comments to the correct answer:
This is how you add a rule to iptables:

阅读您对正确答案的评论:
这是向 iptables 添加规则的方式:

iptables -A INPUT -s 0/0 -p tcp --dport 5432 -j ACCEPT

0/0: If you want anybody to access it.
You can change it to a specific ip address or range of ip addresses.

0/0:如果你想让任何人访问它。
您可以将其更改为特定的 IP 地址或 IP 地址范围。

回答by Nikunj Kakadiya

If you are connected to the server using UI you can run the below query to get the IP Address of server using below query to verify if you are using the correct IP.

如果您使用 UI 连接到服务器,则可以运行以下查询以使用以下查询获取服务器的 IP 地址,以验证您是否使用了正确的 IP。

Select inet_server_addr();