I recently setup a java application in my spring-boot. I'm getting this error when i run it:
org.postgresql.util.PSQLException: The connection attempt failed.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:315)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:51)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:225)
at org.postgresql.Driver.makeConnection(Driver.java:465)
at org.postgresql.Driver.connect(Driver.java:264) ........
Caused by: java.net.UnknownHostException: postgres ........
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2023-02-03 17:24:15 - Application run failed
My application-dev.properties file in src/main/resources/ has this code:
spring.datasource.url=jdbc:postgresql://localhost:5432/sofra_hybrid_7?useUnicode=yes&characterEncoding=UTF-8
spring.datasource.username=postgres
spring.datasource.password=admin
I have made the database too as well in the postgres server with username "postgres" and pass "admin:
lalarukh=# CREATE DATABASE sofra_hybrid_7;
CREATE DATABASE
lalarukh=# grant all privileges on database sofra_hybrid_7 to postgres ;
GRANT
I have even also used the jdbc postgres jar file inside the application to make the connection but all in vain. Seeking Help!
I can't run my java application as postgres can't make connection with my app in spring boot.
CodePudding user response:
I also faced the same issue and after a lot of working I finally resolved it this way: As you're receiving unknown host means that your application can't detect your this file: application-dev.properties
You shall make this application.yml inside your project (yourprojectdirectory/application.yml) and this will automatically detects the DB information and all. As you're using postgres as DB so, add this code inside this file:
spring:
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql:db_name
username: your-postgres-username
password: your-postgres-password
Run this command for application running: mvn spring-boot:run -Dspring-boot.run.profiles