0

This is what I'm having problems with in my application.yml file:

url: ${DATASOURCE_URL:jdbc:postgresql://localhost:5432/postgres}

It seems to treat it like it should cut it at the last colon:

Message: Invalid JDBC URL [5432/postgres]. JDBC URLs must start with 'jdbc'.

Tried escaping with ', " and \ in different sections, nothing seems to work. Tried even combining them

url: "${DATASOURCE_URL:jdbc\\:postgresql\\://localhost\\:5432/postgres}"

and

url: "${DATASOURCE_URL:jdbc\:postgresql\://localhost\:5432/postgres}"

Edit: this value needs to be empty if not defined due to Micronaut Test Resources I'm using. It will create test resources only if value is empty or not defined.

Edit: realized I'm using micronaut instead of spring boot. No wonder the suggestions didn't work.

Edit: I can't omit the url configuration, as I have two different datasources. Omitting would only work for the default one AFAIK - I have to configure the additional datasource somehow, which is what I'm trying to achieve here.

eis
  • 51,991
  • 13
  • 150
  • 199

3 Answers3

1

It might need to declare 'host' and 'port' separately.

Try this: url: jdbc:postgresql://${DB_HOST:localhost}:${DB_PORT:5432}/postgres

  • this does the trick for now. Thank you! – eis Jun 10 '23 at 07:06
  • however, for [Micronaut Test Resources](https://micronaut-projects.github.io/micronaut-test-resources/latest/guide/) that value should be empty if not defined so not accepting this unfortunately as it doesn't resolve this – eis Jun 10 '23 at 07:08
1

For Micronaut, your url configuration with a placeholder "DATASOURCE_URL" and a default value should look like:

url: ${DATASOURCE_URL:`jdbc:postgresql://localhost:5432/postgres`}

Uses backticks "`" to escape the JDBC URL because of the colon ":".

If you are using Micronaut Test Resources, you can omit the URL configuration all together.

datasources:
  default:
    driver-class-name: org.postgresql.Driver # Used by Micronaut Data
    dialect: POSTGRES 
    #schema-generate: CREATE_DROP
    db-type: postgres # Used by Micronaut Test Resources     
ShingJo
  • 614
  • 1
  • 7
-1

Here you try to declare a spring variable by assigning it a default value. So you have to give your variable the name of the path of your application.yml attribute.

For instance, I see your attribute is named "url" : If your application.yml looks like that

yourapp:
  datasource:
    url: ${yourapp.datasource.url:default_value}

Your var in the ${} should be named : ${yourapp.datasource.url:default_value}

elligar34
  • 11
  • 3
  • no, I don't think that's how it works. My variable works just fine as it is, the problem is with special characters in the default value. – eis Jun 09 '23 at 20:25