如何将Spring Boot Web App连接到Postgresql数据库

时间:2019-02-22 18:28:38

标签: java postgresql spring-boot ubuntu firewall

我正在从事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

1 个答案:

答案 0 :(得分:0)

#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