How to use Heroku Postgres with Koyeb Serverless Platform
6 minIntroduction
Heroku provides managed Postres databases and makes them accessible over the Internet. Whether you are an existing Heroku user wanting to deploy your apps globally while keeping your database on Heroku or looking for a place to host a managed Postgres database, this guide explains how to use and connect a Heroku Postgres database from an application running on Koyeb.
Heroku allows you to create Heroku Apps using add-ons. Since its managed PostgreSQL database is an add-on, this means you can create a Heroku app that only runs the Postgres database without having to deploy your entire application on Heroku.
In this guide, we will create a Heroku App with a Heroku Postgres add-on, create a simple Node.js application to connect the database and deploy the application on Koyeb.
Requirements
For this guide, you need:
- A Koyeb account to deploy and run the Node application
- A Heroku account to run the Heroku Postgress add-on
- The Koyeb CLI installed to interact with Koyeb from the command line
- A registry that will store our Node app Docker image that will be deployed on Koyeb
Steps
To use and connect a Heroku Postgres database on Koyeb, you need to follow these steps:
- Create a Heroku App with the Postgres add-on
- Write a Node.js app connection to the Heroku Postgres database
- Deploy the Node.js app on Koyeb
Create a Heroku App with the Postgres add-on
In the Heroku control panel, on the top right navigation bar, click the Create button and select Create new app. You land on the App creation form where you need to:
- Give your application a name
- Choose the region your App will be located: Europe or the United-States
Once the form is filled in, click the Create app button. Then click the Resources tab and in the Add-ons section of the page fill the search input with Heroku Postgres and validate. A modal opens and asks you to select the plan you want to use. In this guide, we use the Heroku Postgres add-on free tier Hobby Dev. Once the plan you want to use is selected, click Submit order form.
Write a Node.js app to connect to the Heroku Postgres database
With the Heroku Postgres add-on deployed, we will now create a basic Node application to connect the database. We will then deploy this application on Koyeb.
Initialize the environment
Create the app
In the node-demo
directory, create a new file app.js
and paste the snippet below.
The app uses Express and Sequelize to connect the Heroku database and expose an API endpoint on /
to return Todo mock data from the database.
Write the Dockerfile and build the Docker image
To Dockerize our Node.js app, you need to create a Dockerfile
in your project folder containing the content below.
To build and properly tag the Docker image execute the following command:
In this guide, we will push the Docker image to a GitHub container registry. You are free to use another different registry as Koyeb allows you to deploy from any container registry.
Once the build is over, you can run a container using the image locally to validate everything is working as expected running:
As in the previous step, you can perform a curl request to ensure the app is running as expected:
Push the Docker image to the GitHub container registry
With the Docker image built and functional, we can now upload it to the GitHub container registry. In your terminal run the command below to push the image:
Once the push command is completed, the Docker image is stored in your GitHub container registry.
Deploy the Node.js app on Koyeb
We are now ready to deploy our application on Koyeb. First, create a Koyeb Secret to store your container registry configuration. In this guide, we will deploy our app from the GitHub container registry. For other container registries examples, check out this related documentation.
If your Docker image is public, there is no need to create a secret containing your container registry configuration.
Then, create a second secret to securely store your Heroku Postgres database connection string. You can retrieve the information from the Heroku control panel, selecting your database, clicking the Settings tab and clicking the View credentials button.
Copy the URI and create the Koyeb secret to securely store running in your terminal:
We are now ready to deploy our application, in the terminal run:
This command creates a new Koyeb App and deploys our Node application exposing port 3000 and making it publicly accessible on the /
route of your Koyeb App URL.
To retrieve your Koyeb App URL and access your application, run:
Open the URL in your browser to access your application running on Koyeb. The application returns the mock Todo data stored in the Heroku Postgres database. By running your application on Koyeb you benefit from native autoscaling, automatic HTTPS (SSL), auto-healing, and global load-balancing across our edge network allowing you to make your applications faster and distribute your content near your users thanks to Koyeb's 55 edge locations.