我正在开发一个Spring启动项目,我试图将我的服务器连接到postgresql数据库。当我使用我的IDE(在我的Windows操作系统上)运行程序时,它正在工作,但是当我制作一个jar文件并在托管我的数据库的ubuntu服务器虚拟机上运行它时它不起作用。
org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
我检查了我的防火墙规则,但仍然没有成功。
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
我做了一些研究,我听说要修改postgresql的配置文件,但它仍然不起作用,我不知道问题出在哪里。
在postgresql.conf上修改的行下面:
listen_addresses = '*' # what IP address(es) to listen on;
ssl = on
并为pg_hba.conf:
local all postgres peer
local all kapitax trust
# TYPE DATABASE USER ADDRESS METHOD
#host all kapitax * password
#host all all 0.0.0.0/0 md5
# "local" is for Unix domain socket connections only
local all all * trust
# IPv4 local connections:
host all all * password
# IPv6 local connections:
host all all ::1/128 password
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all peer
host replication all * md5
host replication all ::1/128 md5
问题可能来自applcation.properties代码?
spring.datasource.url= jdbc:postgresql://localhost:5432/kapitax
spring.datasource.username=kapitax
spring.datasource.password=kapitax
spring.jpa.hibernate.ddl-auto=create-drop
server.port=8090
这条线
#host all all 0.0.0.0/0 md5
应启用如下:
host all all 0.0.0.0/0 md5
使用0.0.0.0/0
,您将收听所有IPv4地址。更改此配置后,请确保重新加载服务器设置或重新启动数据库服务器。更详细的解释可以在以下答案中找到:How to configure PostgreSQL to accept all incoming connections