How To Configure Database Connection in ACC?
This tutorial explains how to configure a database connection for your application in ACC.
Recap: The akinon.json
File
akinon.json
FileEarlier, we created a basic akinon.json
configuration file:
Configuring Addons
Below is the method to specify the addons required for the application within the akinon.json file:
After completing the configuration, the akinon.json file will resemble the following:
Using Environment Variables for Configuration
When a database addon is added, the following environment variables become available to your app:
DB_HOST
DB_PORT
DB_NAME
DB_USER
DB_PASSWORD
Update your settings.py
to use these variables:
To run the app locally, you’ll need to simulate these environment variables:
.env
To enable the application to communicate with PostgreSQL, users must incorporate the postgresql-dev library into the build.sh file.
Below is the modified version of the build.sh file:
Automating of Database Migrations
To ensure database migrations run automatically during deployment, add the migration command to the release
script in akinon.json
:
Below is the addition to be made in the akinon.json file:
Ensuring the Database Connection with Health-Check
To control the database connection and ensure that the application is running smoothly, users can create a healthcheck page. This page will serve as a status indicator, providing information about the application's proper configuration and functionality.
If everything is functioning well, the server will return an HTTP 200 OK status code. Otherwise, if there's an issue, it will handle the error and respond with an appropriate error status code (e.g., 500 Internal Server Error).
Let's add a new view to the application that will respond to the /healthz path and implement the health-check functionality:
views.py
urls.py
Test the healthcheck page in the local environment:
Everything seems to be running smoothly! Let's try pausing the PostgreSQL service locally:
If the application is unable to connect to the database, the health check page will notify users of the issue.
By defining the health check path in the akinon.json
file, ACC can automatically and periodically verify the application's health once it's deployed.
To enable this, simply add the healthcheck
key under the formation > web
section, specifying the path to the health check endpoint.
To test the changes in the ACC environment, push the updates to your application’s repository and trigger a new version build. Once the build is complete, proceed with the deployment.
After deployment, navigate to the health check endpoint. If everything is configured correctly, the page will confirm a successful database connection—indicating that your application is running smoothly.
Last updated
Was this helpful?