繁体   English   中英

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

[英]How to connect spring boot web app to postgresql database

我正在一个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地址。 更改此配置后,请确保重新加载服务器设置或重新启动数据库服务器。 可以在以下答案中找到更详细的说明: 如何配置PostgreSQL以接受所有传入的连接

暂无
暂无

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

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