Reputation: 613
I have a Java webapp running successfully on Tomcat.
I have created three environment variables in my myapp deployment:
MY_ENV_VAR_1=dbuser
MY_ENV_VAR_2=dbpassword
MY_ENV_VAR_3=dbname
I can get these values using standard Java code:
myEnvVar1 = System.getenv("MY_ENV_VAR_1"); myEnvVar2 = System.getenv("MY_ENV_VAR_2"); myEnvVar3 = System.getenv("MY_ENV_VAR_3");
I used 'normal Java' code to connect to the database:
connection = DriverManager.getConnection("jdbc:postgresql://" + postgresqlServiceHost + ":" + postgresqlServicePort + "/" + myEnvVar3, myEnvVar1, myEnvVar2);
So, I can connect to the database successfully.
The problem is:
I can see (in web console) but cannot get the values from the three environment variables in the postgresql deployment:
POSTGRESQL_USER
POSTGRESQL_PASSWORD
POSTGRESQL_DATABASE
Using the 'normal' System.getenv("POSTGRESQL_USER"); does NOT work.
So, please can someone tell me how to access the values of these three postgresql pod environment variables using Java Code?
Upvotes: 0
Views: 1036
Reputation: 613
So, the simple answer is:
You manually add the following three environment variables to the deployment environment of the application that will be accessing the database.
POSTGRESQL_USER
POSTGRESQL_PASSWORD
POSTGRESQL_DATABASE
You must do it in exactly the same way as you see them in the postgresql deployment environment.
It really is that simple...
In Java app:
String postgresqlServiceHost = System.getenv("POSTGRESQL_SERVICE_HOST");
String postgresqlServicePort = System.getenv("POSTGRESQL_SERVICE_PORT");
String postgresqlDatabase = System.getenv("POSTGRESQL_DATABASE");
String postgresqlUser = System.getenv("POSTGRESQL_USER");
String postgresqlPassword = System.getenv("POSTGRESQL_PASSWORD");
Connection connection = DriverManager.getConnection("jdbc:postgresql://" + postgresqlServiceHost + ":" + postgresqlServicePort + "/" + postgresqlDatabase, postgresqlUser, postgresqlPassword);
Upvotes: 1