Cannot connect my Spring Boot to postgres
victorantunes
TRIALOP

2 years ago

Hello

This is my application.yml on a simple test application. The logs say the application cannot connect to the database. Can you please help me on how to proceed?

Thanks in advance

spring:
  datasource:
    url: jdbc:postgresql://${PGHOST}:${PGPORT}/${PGDATABASE}
    username: ${PGUSER}
    password: ${PGPASSWORD}
    driver-class-name: org.postgresql.Driver

This is my ENV

{

"PGDATABASE": "${{Postgres.PGDATABASE}}",

"PGHOST": "${{Postgres.PGHOST}}",

"PGPASSWORD": "${{Postgres.PGPASSWORD}}",

"PGPORT": "${{Postgres.PGPORT}}",

"PGUSER": "${{Postgres.PGUSER}}"

}

View Deploy details

ⓘ Deployment information is only viewable by project members and Railway employees.

9 Replies

brody
EMPLOYEE

2 years ago

But what errors are you getting?


victorantunes
TRIALOP

2 years ago

As per the logs, this is the most important message:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaSharedEM_entityManagerFactory': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument

Which usually means that the backend could not connect to the database. If i hardcode the postgres values in my local machine, it works. But if I export the variables and define them in my application.yml, it doesn't work


brody
EMPLOYEE

2 years ago

As per the logs

Moderators can not see your logs.


brody
EMPLOYEE

2 years ago

This wouldn't be an issue with the platform, it would be a misconfiguration, please research this error and apply the fixes suggested from sites like stack overflow.


victorantunes
TRIALOP

2 years ago

I agree, it is not an issue with the platform. As I said, if I hardcode the values, they will work fine and the application connects to the railway postgres

Is there anything wrong with the way I've set up the variables? I'm asking here because this is a very simple example, and variable misconfiguration inside Railway is the only thing I can think of right now, since there's not much room to go wrong here with the application code.


brody
EMPLOYEE

2 years ago

I'm assuming you do have the needed variables set in the variables tab of your service?


victorantunes
TRIALOP

2 years ago

I've added the variables that postgres exports into my application

Is this the right way of doing it?


brody
EMPLOYEE

2 years ago

Looks good to me, you just need to figure out why your app is not reading the environment variables.
Are you sure java does variable expansion when loading values from a application.yml file? or maybe you are not using the correct syntax?


joaokbral
TRIAL

a year ago

I think I know why it's not working, spring can't read you .env files, you need to import it for the spring application.properties, you should
have:

spring.config.import: optional:file:.env[.properties]

It'll probably work. You need to have the .properties, didn't really read to much about it but I'm pretty sure it's related to the file extension.


Loading...