简体   繁体   English

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

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

I'm working on a Spring boot project and i tried to connect my server to a postgresql database. 我正在一个Spring引导项目上,试图将服务器连接到PostgreSQL数据库。 When i run the program with my IDE (on my windows OS) it's working but when i make a jar file and run it on my ubuntu server virtual machine which host my DB it doesn't work. 当我使用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.

I've check my firewall rules but still no sucess. 我已经检查了防火墙规则,但仍然没有成功。

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

I make some research and I heard about the configuration files of postgresql to modify but it still doesn't work and i have no idea where the issue comes. 我做了一些研究,听说了要修改的postgresql的配置文件,但它仍然无法正常工作,我也不知道问题出在哪里。

Below the lines that i've modified on postgresql.conf: 在我对postgresql.conf修改的行下面:

listen_addresses = '*'          # what IP address(es) to listen on;
ssl = on

and for the pg_hba.conf: 对于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

The issue can comes from the applcation.properties code ? 问题可能来自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

The line 线

#host   all             all             0.0.0.0/0               md5

should be enabled like: 应该启用如下:

host   all             all             0.0.0.0/0               md5

With 0.0.0.0/0 you will listen for all IPv4 addresses. 使用0.0.0.0/0您将侦听所有IPv4地址。 After you change this configuration make sure to reload your server settings or restart the database server. 更改此配置后,请确保重新加载服务器设置或重新启动数据库服务器。 A more detailed explanation can be found in the following answer: How to configure PostgreSQL to accept all incoming connections 可以在以下答案中找到更详细的说明: 如何配置PostgreSQL以接受所有传入的连接

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

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