Cannot connect to PostgreSQL database from Spring Boot application - FATAL: database does not exist - postgresql

I just configured new project with https://start.spring.io/
My pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>tower</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>tower</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>9</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
and my application.properties:
spring.datasource.url= jdbc:postgresql://localhost:5432/tower
spring.datasource.username=postgres
spring.datasource.password=XXX
spring.jpa.hibernate.ddl-auto=none
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false
I got an error org.postgresql.util.PSQLException: FATAL: database "tower" does not exist. It says that my database is not up but I can easily get into it with pgAdmin3/4 and runs queries. I also use localhost:5432 with database name tower, user postgres and my password which is correct for sure. Why then I got and error that my database is not up.
I also tried:
Class.forName("org.postgresql.Driver");
Connection connection = null;
connection = DriverManager.getConnection(
"jdbc:postgresql://localhost:5432/tower","postgres", "XXX");
and got exactly the same error- database "tower" does not exists.
Why I got such an error when database is up?

Turn on logging, at least you will see if you're trying to connect in Postgesql. Also try without using PGAdmin, like using the Squirrel SQL client which, unlike PGAdmin, will require you to set up the connection exactly how you describe ie URL = jdbc:postgresql://localhost:5432/tower
If Squirrel connects then it's more like your code than PGAdmin

I know this is an old question but what worked for me is creating the db tower

Related

Can't import Spring Cloud dependencies for enabling Eureka client

I work on app in which I try to register Eureka client on server but stuck on the first step where I need dependency which make enable #EnableEurekaClient annotation.
I followed cloud.spring.io instructions which did not work:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
II) I then found some latest stack & tried (as follows) which did not work as well:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
I wonder, as all folder suddenly disappear in case I put above dependency in the pom.xml (I use Intellij)
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-
4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.eurekaproject.learn</groupId>
<artifactId>microservice-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>microservice-app</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.yml
spring:
application:
name: microservice-app
datasource:
url: jdbc:mysql://localhost:3306/microservice-app
username: root
password: 12345
jpa:
hibernate:
ddl-auto: none
server:
port: 8092
eureka:
client:
service-url:
defaultZone: http://localhost:8081/eureka
info:
app:
name: ${spring.application.name}
You need to add the Spring Cloud BOM to your pom.xml as follows:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2020.0.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Once this is added, all Spring Cloud dependencies including spring-cloud-starter-netflix-eureka-client will resolve, and you should be able to use annotations such as #EnableDiscoveryClient for activating the Eureka discovery client.

Spring Cloud Config Server OAuth2 authorization

I have a config server and spring boot microservices running with config server requiring basic authentication user/pw. I have a Keycloak server I want to use for authentication and authorization of the microservice to the config server.
I have added the keycloak spring boot adapter to the config server so now the config server requires an OAuth2 token and have verified with Postman that it works as expected.
This is my microservice pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.7.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo-3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo-3</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
<spring-cloud.version>Hoxton.SR9</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
this is my bootstrap.yml
spring:
application:
name: appAuthzRestSpringBoot
cloud:
config:
uri: ${SOA_CONFIG_URI}
fail-fast: true
security:
oauth2:
client:
registration:
keycloak:
authorization-grant-type: client_credentials
client-id: app-authz-rest-springboot
client-secret: secret
provider:
keycloak:
token-uri: http://localhost:8080/auth/realms/spring-boot-quickstart/protocol/openid-connect/token
The bootstrap call to the config server does not have an OAuth2 bearer token.
What am I missing?

In pom .xml it's showing error: Failure to transfer org.apache.maven.shared:file-management:pom:3.0.0

I created the project from Spring Initializr: https://start.spring.io/.
After importing it as a maven project in eclipse it's showing error in the pom.xml file that:
Description Resource Path Location Type Failure to transfer
org.apache.maven.shared:file-management:pom:3.0.0 from
https://repo.maven.apache.org/maven2 was cached in the local
repository, resolution will not be reattempted until the update
interval of central has elapsed or updates are forced. Original error:
Could not transfer artifact
org.apache.maven.shared:file-management:pom:3.0.0 from/to central
(https://repo.maven.apache.org/maven2):
repo.maven.apache.org pom.xml /SpringApp line 1 Maven Configuration
Problem
Error marked only the first line of code.
My pom.xml :
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
</parent>
<groupId>com.example</groupId>
<artifactId>SpringApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringApp</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
This is due to corrupted/failed downloads which are causing the problem to update your maven project.
If you are a windows user please run this command in CMD
cd %userprofile%\.m2\repository
for /r %i in (*.lastUpdated) do del %i
this will delete all the failed downloads from the local repository.
after this completes you can update dependencies of your project by
Right-click on the project->Maven->Update Project->check the checkbox "Force Update of Snapshots/Releases" and click OK.

When I generate Spring starter project with eclipse STS tools there is an error occurs in pom.xml first line. But I found no error in pom.xml

I have created a Spring starter project with eclipse STS tool after creating project there is an error in pom.xml first. But I found no error. I have tried Updating project with and with out forcefully but error still persists
Thank you
<?xml version="1.0" encoding="UTF-8"?> <!-- error occurs here -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.web-app</groupId>
<artifactId>FirstWebApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>FirstWebApp</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Project runs but not working.
[This is the image of eclipse workspace] [1]: https://i.stack.imgur.com/a9fGB.png
Well , this is the latest well known problem of Eclipse when working with Spring Boot 2.1.5.
The workaround is to add the following to pom.xml :
<properties>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
</properties>

Spring boot admin server not showing u\eureka clients registered on eureka server

Below is my eureka server application main class
package com.example.restaurant.server.startup;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
#EnableEurekaServer
#SpringBootApplication
#EnableAdminServer
public class RestaurantEurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(RestaurantEurekaServerApplication.class, args);
}
}
Here is the application.properties file for same
spring.application.name=RestaurantEurekaServer
eureka.client.service-url.defaultZone=http://localhost:9091/
server.port=9091
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
security.basic.enabled= false
management.security.enabled= false
eureka.instance.health-check-url=/actuator/health
eureka.instance.home-page-url=/actuator/info
spring.boot.admin.context-path=/admin
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
The problem is my eureka server dashboard shows the clients registered on it but when i navigate to spring boot admin,it shows 0 applications registered
Pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>RestaurantEurekaServer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
<start-class>com.example.restaurant.server.startup.RestaurantEurekaServerApplication</start-class>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/de.codecentric/spring-boot-admin-server -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.0.1</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
in application.properties, the eureka.client.fetch-registry should be true. With this, SBA can fetch the registration from eureka server.
Okay so after working on it for couple of days i figured it out
I had not set spring.boot.admin.client.url property in client due to which client was not aware on which server admin is running.
So 2 properties that i added in client are
spring.boot.admin.client.url=http://localhost:9095
spring.boot.admin.client.name=""//client will be registered in spring boot admin with his name
Also added maven dependency for spring boot admin in each client.
I think your problem is:
eureka.client.service-url.defaultZone=http://localhost:9091/
needs to be:
eureka.client.service-url.defaultZone=http://localhost:${server.port}/eureka/
you need to set in server
eureka.client.fetch-registry=true
and
#EnableDiscoveryClient
and you dependencies below is wrong:
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.0.1</version>
</dependency>
only need dependency:
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.2.0</version> # change to your case
</dependency>