Deploy Ktor App with Postgresql on Heroku
So In this article we are going to learn how to deploy ktor app with postgresql database on Heroku server 😍 . I have already create a ktor application , if you want to get access of the project click here.
first we need to install heroku cli (command line interface) on your system 😉, through cli we will deploy ktor app on server.
For mac and linux installation , paste this command on terminal
brew tap heroku/brew && brew install heroku
for window installation , go to this website and download
Let’s check , is heroku cli installed on your system or not ? type the below command on terminal
heroku --version
Now login into heroku through below command.
heroku login
Now it’s time to start the deployment process on heroku server , so go to your project , open build.gradle file and add the shadow plugin or dependencies and start sync.
So What’s shadow plugin does ? 🤔
So shadow plugin will create a fat jar file. The fat jar is the jar, which contains classes from all the libraries, on which your project depends and, of course, the classes of current project.
Now double click on build and jar file and wait to finish the build 👇🏻
Now you can run gradle command to build the jar file in the build/libs folder.
./gradlew build
Go to root folder -> build -> libs -> your jar file (In my case example-0.0.1-all.jar)
Now create a Procfile file on root folder of your project,and paste the below code.
web: java -jar build/libs/example-0.0.1-all.jar
Now create a .env file on root folder and paste the system environment credentials. This will helps you to run ktor app locally.
Now run the ktor app locally .. paste the below command on terminal
heroku local:start
and boom guys finally your ktor app running locally 🤩 and now it’s time to deploy and run app on heroku server.
First we need to create a git repository , where we push our code 😊
Go to VCS -> enable version control
Enter the below command to create repository
heroku create project_name
Then you have to set GRADLE_TASK in Heroku config vars , run this command on terminal.
heroku config:set GRADLE_TASK="shadowJar"
Now we will create a database for the heroku , where we will store our data..
heroku addons:create heroku-postgresql:hobby-dev
hobby-dev is a free database plan . Finally we have done with all the steps, now push the code on heroku master.
Go to your heroku sever , open the project -> go to setting
Click on Config Vars and add your system environment variable credentials.
Now you can test your api on postman 😍 click on open app
Hope you enjoyed the article , if you face any issue while deploying feel free to ask in the comment section , definitely will help you 😊 Keep learning happy coding ❤️