27

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
  • 293
  • 1
  • 3
  • 9

3 Answers3

57

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
  • 728
  • 4
  • 6
15

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
  • 23,729
  • 8
  • 95
  • 129
  • 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
9

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
  • 816
  • 11
  • 8