6

I am having trouble getting values from my environment variables.. There are many simliar questions. But NONE of them worked for me

Application.properties

spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=${SPRING_DATASOURCE_USERNAME}
spring.datasource.password=${SPRING_DATASOURCE_PASSWORD}

System variables

Variable name : SPRING_DATASOURCE_USERNAME
Variable Value : dbuser

Variable name : SPRING_DATASOURCE_PASSWORD
Variable Value : 123456789

ERROR

invalid username/password; logon denied

but when I hard code it, it works fine.

Update

JayC
  • 2,144
  • 8
  • 42
  • 77
  • Possible duplicate of [Using env variable in Spring Boot's application.properties](https://stackoverflow.com/questions/35531661/using-env-variable-in-spring-boots-application-properties) – Alex R Oct 11 '18 at 02:47
  • isn't this redundant? spring.datasource.username=${SPRING_DATASOURCE_USERNAME} – Anatolii Stepaniuk Jul 09 '20 at 08:55

3 Answers3

8

After defining values in system.environment variables. You must restart eclipse so it can take action, Otherwise it will not read the recently set values.

JayC
  • 2,144
  • 8
  • 42
  • 77
5

If you have set your environmvent variables correctly that should work. For example: export SPRING_DATASOURCE_USERNAME=root

You can also set the properties spring.datasource.username and spring.datasource.password via the environment variables SPRING_DATASOURCE_USERNAME and SPRING_DATASOURCE_PASSWORD. Then you don't have to write anything about that into your application.properties.

See also the Spring-Boot Documentation about externalized configuration: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

David Schilling
  • 2,442
  • 3
  • 17
  • 24
  • I did what you said and removed the user name and password from application.properties. And only have it defined in the environment. I receive the same error. – JayC Feb 23 '17 at 22:03
  • How did you set the environment variables? When i'm setting them like this it works: `export SPRING_DATASOURCE_USERNAME=root` – David Schilling Feb 23 '17 at 22:21
  • 1
    why you have "export"? I went to my windows typed environment varibles. and manually added it. – JayC Feb 24 '17 at 14:29
  • Ah i see you are using windows. How do you start the application? I think the problem is that your environment variable is not set probably. – David Schilling Feb 24 '17 at 15:04
  • mvn spring-boot:run. I guess my application properties is not reading the environment variables that I set. – JayC Feb 24 '17 at 15:15
  • @Jesse, I think David used Linux OS - thus: export SPRING... – Daniel Klimuntowski Aug 24 '20 at 10:08
0

Late to the party, but I had to restart not only the IntelliJ IDEA but also my computer in order to make it work.

The environment variables for the data source should be named:

SPRING_DATASOURCE_URL
SPRING_DATASOURCE_USERNAME
SPRING_DATASOURCE_PASSWORD

If you name them this way, you don't even have to declare the spring.datasource.url, spring.datasource.username and spring.datasource.password properties anymore, since Spring will auto-detect those environment variables and will create the DataSource with the data extracted from them.