skip to Main Content

SO i have a spring boot application, and configured postgresql, as below

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.datasource.url=jdbc:postgresql:dburl
spring.datasource.username=admin
spring.datasource.password=XXX

But I get failure on application start up that a datasource is not created

No qualifying bean of type 'javax.sql.DataSource' available

I decided to create a datasource bean instead:

@Configuration
public class DataSourceConfig {

    @Bean
    public DataSource dataSource() {
        // Create and configure the DataSource here
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("org.hibernate.dialect.PostgreSQLDialect");
        dataSource.setUrl("jdbc:postgresql://localhost:5432/your-database");
        dataSource.setUsername("your-username");
        dataSource.setPassword("your-password");

        return dataSource;
    }
}

This seems to create the datasource connection.

But i dont understand why i cannot create the datasource from the properties.

2

Answers


  1. Can you please show us your dependencies? If using Maven as your build tool, please check to see if you have this (please add, if you don’t and try again):

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    
    Login or Signup to reply.
  2. In your given sample you are missing port number for Postgres.

    It is very simple to connect to Postgres using spring boot. Try following –

    pom.xml

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    
    <dependency>
      <groupId>org.postgresql</groupId>
      <artifactId>postgresql</artifactId>
      <scope>runtime</scope>
    </dependency>
    

    application.properties

    spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
    spring.jpa.hibernate.ddl-auto=update
    spring.jpa.hibernate.show-sql=true
    spring.datasource.url=jdbc:postgresql://localhost:5432/my-db
    spring.datasource.username=postgres
    spring.datasource.password=root
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search