32

I've got a working Spring Boot Application that connects to a Postgres database. I've got the project set up with an application.properties file, but would like to make the switch over to an application.yml file. However when I make the switch, my application errors out while attempting to connect to the db.

Original applications.properties file:

spring.jpa.database=POSTGRESQL
spring.datasource.platform=postgres
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.database.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=foo
spring.datasource.password=bar

And Here's what I've got so far in the application.yml file:

spring.jpa:
  database: POSTGRESQL
  hibernate.ddl-auto: create-drop
  show-sql: true

spring.datasource:
  platform: postgres
  driverClassName: org.postgresql.Driver
  url: jdbc:postgresql://localhost:5432/mydb
  username: foo
  password: bar

Am I missing something in the translation between file types?

user3051261
  • 343
  • 1
  • 4
  • 9

5 Answers5

65

You need to treat each . character in property names as levels in the yaml file:

spring:
  jpa:
    database: POSTGRESQL
    show-sql: true
    hibernate:
      ddl-auto: create-drop
  datasource:
    platform: postgres
    url: jdbc:postgresql://localhost:5432/mydb
    username: foo
    password: bar
    driverClassName: org.postgresql.Driver

EDIT: edits have been suggested, thanks for that. The driverClassName property actually should be under spring.datasource. However, the purpose of this answer was to show how a properties file is converted into yaml format. So I have changed the driverClassName property to be at the right path, that is not part of the transformation from properties to yaml.

Zoltan
  • 808
  • 5
  • 6
18

Please upvote the other answer (Z0lt@n's answer)

But pasting here for future readers... a sql server version.

spring:
  jpa:
    show-sql: true
    hibernate:
      ddl-auto: update
    properties:
      hibernate.jdbc.batch_size: 20
      hibernate.cache.use_query_cache: false
      hibernate.cache.use_second_level_cache: false
      hibernate.cache.use_structured_entries: false
      hibernate.cache.use_minimal_puts: false
  datasource:
    #SPRING_DATASOURCE_URL environment variable will be something like -> jdbc:sqlserver://MySqlServer\\MyInstance:1433;DatabaseName=MyDbName;
    url: ${SPRING_DATASOURCE_URL}
    username: ${SPRING_DATASOURCE_USERNAME}
    password: ${SPRING_DATASOURCE_PASSWORD}
    driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver

and maven entry

    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>mssql-jdbc</artifactId>
        <version>7.0.0.jre8</version>
    </dependency>

APPEND

This seems to be the "standard" name for the driverClassName.

SPRING_DATASOURCE_DRIVER-CLASS-NAME

And of course, in my example, you would use the value of:

com.microsoft.sqlserver.jdbc.SQLServerDriver

NOW, some spring, springboot, environment variable voodoo alert.

Sometimes, when specifying the environment variable...for some command lines items, I would have to change the hyphens and make them underscores. (aka, "SPRING_DATASOURCE_DRIVER-CLASS-NAME" vs "SPRING_DATASOURCE_DRIVER_CLASS_NAME"

below the -e generically represents "passing environment variable values via the command line"

MyCommandLineProgram.exe -e SPRING_DATASOURCE_URL="jdbc:sqlserver://myServerName:1433;DatabaseName=MyDB;" -e SPRING_DATASOURCE_USERNAME="myUserName" -e SPRING_DATASOURCE_PASSWORD="myPassword" -e SPRING_DATASOURCE_DRIVER_CLASS_NAME="com.microsoft.sqlserver.jdbc.SQLServerDriver"

There's some voodoo for you.

Those interested in the logging (logback.xml) issue, maybe want to find my answer here as well:

Spring Boot Logback DB Appender Properties

granadaCoder
  • 26,328
  • 10
  • 113
  • 146
  • so just specifying SPRING_DATASOURCE_URL (and others) env var is not enough for Datasource? you need to explicitly set spring.jpa.datasource.url parameters? – Anatolii Stepaniuk Jul 09 '20 at 08:49
  • All the others probably have defaults. ddl-auto is the one I would be explicit about since it can be dangerous in production environment. – granadaCoder Jul 09 '20 at 11:55
11

application.yml file for postgresql

Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)

spring:
  datasource:
    driver-class-name: org.postgresql.Driver
    username: postgres
    password: root
    url: jdbc:postgresql://localhost/postgres
    platform: postgres
    initialization-mode: always
    continue-on-error: true
  jpa:
    show-sql: true
    generate-ddl: true
    hibernate:
      ddl-auto: create
    database: postgresql
Prasenjit Mahato
  • 1,174
  • 15
  • 10
0
server:
  port: 1111  
spring:
    application:
      name: client-one 
    datasource:
     password: postgres
     url: jdbc:postgresql://localhost:5432/postgres?currentSchema=education
     username: postgres
    jpa:
     hibernate:
      ddl-auto: update
     properties:
      hibernate:
        dialect: org.hibernate.dialect.PostgreSQLDialect
     show-sql: true

Dulaj Kulathunga
  • 1,248
  • 2
  • 9
  • 19
razak
  • 1
  • 1
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 21 '22 at 10:03
0

File application.yml

server:
    port: 1234
spring:
    application:
        name: foo
    datasource:
        url: jdbc:postgresql://localhost:5432/foo
        password: postgres
        username: postgres
    jpa:
        hibernate:
            ddl-auto: update
        properties:
            hibernate:
                dialect: org.hibernate.dialect.PostgreSQLDialect
        show-sql: true

Test with PostgreSQL 15.3 , JDK 20, Spring Boot 3.1.0 .

Vy Do
  • 46,709
  • 59
  • 215
  • 313