1

I'm using STS 4.6.2.RELEASE on Mac OS X and am attempting to build a Spring Boot 2 project. I have defined these environment variables in my ~/.bash_profile file ...

export PG_DB_NAME=cardmania
export PG_DB_USER=cardmania
export PG_DB_PASS=password
export PG_DB_HOST=localhost

Then in my src/main/resources/application.properties file, I have defined

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect

spring.jpa.hibernate.ddl-auto=create

spring.jpa.hibernate.show-sql=true

spring.datasource.url=jdbc:postgresql://${PG_DB_HOST:localhost}:5432/${PG_DB_NAME}

spring.datasource.username=${PG_DB_USER}
spring.datasource.password=${PG_DB_PASS}

However, when I start my Spring Boot app from within STS by right-clicking on my project and selecting "Run As" -> "Spring Boot App," I get this error ...

org.postgresql.util.PSQLException: FATAL: role "${PG_DB_USER}" does not exist
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2532) ~[postgresql-42.2.14.jar:42.2.14]
    at org.postgresql.core.v3.QueryExecutorImpl.readStartupMessages(QueryExecutorImpl.java:2644) ~[postgresql-42.2.14.jar:42.2.14]
    at org.postgresql.core.v3.QueryExecutorImpl.<init>(QueryExecutorImpl.java:137) ~[postgresql-42.2.14.jar:42.2.14]
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:255) ~[postgresql-42.2.14.jar:42.2.14]
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49) ~[postgresql-42.2.14.jar:42.2.14]
    at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:217) ~[postgresql-42.2.14.jar:42.2.14]
    at org.postgresql.Driver.makeConnection(Driver.java:458) ~[postgresql-42.2.14.jar:42.2.14]
    at org.postgresql.Driver.connect(Driver.java:260) ~[postgresql-42.2.14.jar:42.2.14]
    at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-3.4.5.jar:na]
    at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:358) ~[HikariCP-3.4.5.jar:na]
    at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206) ~[HikariCP-3.4.5.jar:na]
    at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:477) ~[HikariCP-3.4.5.jar:na]
    at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:560) ~[HikariCP-3.4.5.jar:na]
    at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) ~[HikariCP-3.4.5.jar:na]
    at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-3.4.5.jar:na]
    at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:180) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:68) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:101) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:263) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:237) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
    at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.injectServices(DefaultIdentifierGeneratorFactory.java:152) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.injectDependencies(AbstractServiceRegistryImpl.java:286) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:243) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.<init>(InFlightMetadataCollectorImpl.java:176) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:118) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1224) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1255) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) ~[spring-orm-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:391) ~[spring-orm-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[na:na]
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[na:na]
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[na:na]
    at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]

I do not encounter this error if I run "java -jar target/cardmania-0.0.1-SNAPSHOT.jar" on a normal shell command line.

satish
  • 703
  • 5
  • 23
  • 52
  • Does this help you? https://stackoverflow.com/questions/56782840/spring-sts-not-loading-environment-variables-but-running-maven-works – Kavitha Karunakaran Jun 21 '20 at 18:34
  • Perhaps I'm misreading the answer in your link, but the answer seems to suggest putting the environment variables in the ~/.bash_profile file, which is what I'm already doing. Is there another part of that solution that I should be trying that I am not? – satish Jun 21 '20 at 18:36
  • Did you restart your system after that? Is mvn spring-boot:run working fine after doing a restart? Normally, eclipse and STS which is built on top of eclipse should take up the environment variables set in bash files provided they are sourced properly. To double check and be sure that they are sourced, can you check if echo $PG_DB_USER gives the right output in terminal after the restart? – Kavitha Karunakaran Jun 21 '20 at 18:41
  • Also if nothing from the suggestions above work, can you try adding those variables to /etc/environment too? – Kavitha Karunakaran Jun 21 '20 at 18:50
  • I did restart my system, confirmed I could view the env vars on the shell using "echo $PG_DB_USER" as you suggest and even tried adding the exports as I have them to /etc/enviornment and restarting, but STS still does not recognize them. The application does start fine when just running normally through a command line. – satish Jun 21 '20 at 19:22
  • Does this answer your question? [Environment variables in Eclipse](https://stackoverflow.com/questions/7048216/environment-variables-in-eclipse) – DEWA Kazuyuki - 出羽和之 Jun 22 '20 at 08:51
  • Show output of `echo "$PG_DB_NAME"`. – Cyrus Jun 25 '20 at 16:28

2 Answers2

2

I believe this behaviour has more to do with your IDE than your environment variables configurations. I faced the same problem and found a workaround, Eclipse STS lets you define environment variables for the Run Configuration:

Environment variables in Eclipse

You could check this answer too, maybe it gives you other approaches

CarlosJavier
  • 1,005
  • 1
  • 16
  • 29
  • "behaviour has more to do with your IDE than your environment variables" Actually, I think that's probably not true. My guess is that the Mac OS launcher explicitly 'cleans' the evironment that Eclipse (or any app you launch by dy double-clicking it) sees. The most likely reason for this is security considerations (env vars can contain sensitive data you don't want every app to see). So as a 'general safety measure' Mac OS only passes a limited environment to processes it launches from the UI. – Kris Jun 22 '20 at 16:59
  • Your answer still provides a good workaround though, so upvoted it for that :-). Whatever the reason may be that the STS process is not receiving vars from the bash_profile, it is always possible to use the Eclipse launch to provide them explicitly as you sugested. – Kris Jun 22 '20 at 17:04
0

If you launch STS from Finder, Dock or Spotlight, you should edit ~/.MacOSX/environment.plist, but not ~/.bash_profile.

see also:

If you launch STS from command line(bash), it is not required.

  • Regarding ~/.MacOSX/environment.plist, what you linked to says "Please note that environment.plist does not work for applications launched via Spotlight." However, I tried their suggested solution (putting the environment variables in /etc/launchd.conf) and launching from either Spotlight or the finder does not recognize my env vars when I run "Run As" -> "Maven Install" via STS. – satish Jun 29 '20 at 15:29