JayC
JayC

Reputation: 2292

How to set system environment variables in applicaton.properties the 12 factor way?

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

Upvotes: 6

Views: 33681

Answers (3)

Revnic Robert-Nick
Revnic Robert-Nick

Reputation: 627

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.

Upvotes: 0

JayC
JayC

Reputation: 2292

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.

Upvotes: 8

David Schilling
David Schilling

Reputation: 2780

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

Upvotes: 5

Related Questions