Bash script to extract Quarkus jdbc.url, username and password from Heroku DATABASE_URL

Felipe Windmoller
2 min readJul 18, 2021

--

If you want to deploy a Quarkus application that uses PostgreSQL on Heroku as a container you will have one problem.

When you create a PostgreSQL on your Heroku application, it will only provide the DATABASE_URL environment variable with this pattern:

postgres://username:password@host:port/databasename

The problem is that you need these three variables in your Quarkus application with a different pattern:

quarkus.datasource.jdbc.url=jdbc:postgresql://host:port/databasename
quarkus.datasource.username=username
quarkus.datasource.password=password

You could create three variables on the Heroku Config Vars and receive them on your Quarkus application like this:

quarkus.datasource.jdbc.url=${DB_JDBC_URL:jdbc-url}
quarkus.datasource.username=${DB_JDBC_USER:postgres}
quarkus.datasource.password=${DB_JDBC_PASSWORD:postgres}

That will work, but Heroku gives you the following warning:

Please note that these credentials are not permanent.

Heroku rotates credentials periodically and updates applications where this database is attached.

Bash script

I’ve created a bash script to extract the three Quarkus parameters from Heroku `DATABASE_URL`.

It worked for me with JVM and Native versions.

My application.properties:

quarkus.datasource.jdbc.url=${DB_JDBC_URL:jdbc-url}
quarkus.datasource.username=${DB_JDBC_USER:postgres}
quarkus.datasource.password=${DB_JDBC_PASSWORD:postgres}

Short story

# cut the DATABASE_URL after ‘@’
export DB_JDBC_URL=jdbc:postgresql://${DATABASE_URL#*@}
# substring the DATABASE_URL between ‘//’ and ‘:’
export DB_JDBC_USER=$(expr $DATABASE_URL : ‘.*/\([^:]*\):.*’)
# substring the DATABASE_URL between ‘:’ and ‘@’
export DB_JDBC_PASSWORD=$(expr $DATABASE_URL : ‘.*:\([^@]*\)@.*’)

Long story

Bash script

Save the following script with the heroku.sh file name in the root folder of your project:

Dockerignore

Remember to include this file into your .dockerignore:

*
!heroku.sh
!target/*-runner
!target/*-runner.jar
!target/lib/*
!target/quarkus-app/*

Dockerfile.jvm

Replace the original ENTRYPOINT by this:

# commands from original Quarkus Dockerfile.jvm file suppressed for brevietyCOPY — chown=1001 heroku.sh /deployments/heroku.sh
RUN chmod 540 /deployments/heroku.sh
CMD [ “/bin/bash”, “-c” , “. /deployments/heroku.sh && /deployments/run-java.sh” ]

Dockerfile.native

Replace the original CMD by this:

# commands from original Quarkus Dockerfile.jvm file suppressed for brevietyCOPY — chown=1001 heroku.sh /work/heroku.sh
RUN chmod 540 /work/heroku.sh
CMD [“/bin/bash”, “-c”, “. ./heroku.sh && ./application”, “-Dquarkus.http.host=0.0.0.0”]

Heroku Config Vars

DB_HEROKU_SPLIT

Add the DB_HEROKU_SPLIT to your Config Vars of Heroku and set it to true if you want the bash script to extract the Quarkus data source variables to you.

Set it to false and you can create the Quarkus data source (DB_JDBC_URL, DB_JDBC_PASSWORD and DB_JDBC_PASSWORD) in the Heroku Config Vars and pass them directly to Quarkus bypassing the bash script.

DB_ECHO_VALUES

I also create a DB_ECHO_VALUES variable that allow you to display the data source on the logs. If you want to print the log on the screen, just set DB_ECHO_VALUES to true.

I hope this helps you, but if I did something wrong, please tell me in the comments.

--

--

Felipe Windmoller

Staff Software Engineer at Banco do Brasil | Oracle Certified Professional, Java SE 8 and Java SE 17 | Mainframe and Cloud Developer