How to Deploy Micronaut on Platform.sh with Micronaut Data
Back to home
On this page
Micronaut Data is a database access toolkit that uses Ahead of Time (AoT) compilation to pre-compute queries for repository interfaces that are then executed by a thin, lightweight runtime layer.
Note
This guide only covers the addition of a service configuration to an existing Micronaut project already configured to deploy on Platform.sh. Please see the deployment guide for more detailed instructions for setting up app containers and initial projects.
1. Add a SQL database service
In your service configuration, include a SQL database service. Make sure to visit the documentation for that service to find a valid version. For PostgreSQL that would look like:
postgresql:
type: postgresql:16
disk: 256
2. Grant access to the service through a relationship
To access the new service, set a relationship
in your app configuration.
relationships:
postgresql: "postgresql:postgresql"
3. Export connection credentials to the environment
Connection credentials for services are exposed to the application container through the PLATFORM_RELATIONSHIPS
environment variable from the deploy hook onward. Since this variable is a base64 encoded JSON object of all of your project’s services, you’ll likely want a clean way to extract the information specific to the database into it’s own environment variables that can be used by Micronaut. On Platform.sh, custom environment variables can be defined programmatically in a .environment
file using jq
to do just that:
export JDBC_HOST=`echo $PLATFORM_RELATIONSHIPS|base64 -d|jq -r ".database[0].host"`
export JDBC_PORT=`echo $PLATFORM_RELATIONSHIPS|base64 -d|jq -r ".database[0].port"`
export DATABASE=`echo $PLATFORM_RELATIONSHIPS|base64 -d|jq -r ".database[0].path"`
export DATASOURCES_DEFAULT_PASSWORD=`echo $PLATFORM_RELATIONSHIPS|base64 -d|jq -r ".database[0].password"`
export DATASOURCES_DEFAULT_USERNAME=`echo $PLATFORM_RELATIONSHIPS|base64 -d|jq -r ".database[0].username"`
export DATASOURCES_DEFAULT_URL=jdbc:postgresql://${JDBC_HOST}:${JDBC_PORT}/${DATABASE}
export JAVA_OPTS="-Xmx$(jq .info.limits.memory /run/config.json)m -XX:+ExitOnOutOfMemoryError"
Tip
Environment variable names are following the conversion rules of the Micronaut Documentation.
4. Connect to the service
Commit that code and push. The specified cluster will now always point to the PostgreSQL or any SQL service that you wish.