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 version
# 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 version
Bash script
Save the following script with the heroku.sh file name in the root folder of your project.
#!/bin/sh
# =============================================================================
# This script automatically splits the Heroku ENV DATABASE_URL variable
# into the three JDBC variables needed from Quarkus.
#
# It will only do the split if the DB_HEROKU_SPLIT is set to "true".
#
# If you set DB_HEROKU_SPLIT to 'false', you must pass the Quarkus parameters:
# - DB_JDBC_URL;
# - DB_JDBC_USER;
# - DB_JDBC_PASSWORD.
#
# For test purposes, you can set the DB_ECHO_VALUES to 'true' and check if the
# values are correct.
#
# Pattern of DATABASE_URL from Heroku:
# --------------------------------------
# postgres://username:password@host:port/databasename
#
# Pattern of JDBC variables of Quarkus:
# -------------------------------------
# quarkus.datasource.jdbc.url=jdbc:postgresql://host:port/databasename
# quarkus.datasource.username=username
# quarkus.datasource.password=password
#
# =============================================================================
echo DB_HEROKU_SPLIT=[$DB_HEROKU_SPLIT]
# check for 'true' in string (case insensitive)
if [[ "${DB_HEROKU_SPLIT,,}" == "true" ]]; then
# 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 : '.*:\([^@]*\)@.*')
fi
# check for 'true' in string (case insensitive)
if [[ "${DB_ECHO_VALUES,,}" == "true" ]]; then
echo DATABASE_URL=[$DATABASE_URL]
echo DB_JDBC_URL=[$DB_JDBC_URL]
echo DB_JDBC_USER=[$DB_JDBC_USER]
echo DB_JDBC_PASSWORD=[$DB_JDBC_PASSWORD]
fi
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 breviety
COPY --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 breviety
COPY --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 datasource variables to you.
Set it to false and you can create the Quarkus datasource (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.