So you've designed your database, built it and it's now running locally on your desktop with no errors. The only thing left is deploying to Heroku. Well, this post provides some guidance on how to do that.
-
I'm assuming you already have an account with Heroku and if not, you can sign up here. What you have to do next is install pg, a Postgres client for NodeJs.
npm install pg
-
Add a Knex configuration for production
production: { client: 'pg', connection: process.env.DATABASE_URL, migrations: { directory: './data/migrations', }, seeds: { directory: './data/seeds' }, }
I'll explain where the connection value is coming from later.
Log in to Heroku, click New to create a new app, give your app a name and create it.
Choose a deployment method. Since we're going to be using Heroku commands to run knex migrations, let's use the Heroku Git deployment method. The instructions on how to install and use Heroku CLI are simple and easy to follow.
Once you're through installing Heroku CLI and running the following commands, move to the Resources tab and type Heroku Postgres in the add-ons search bar. Click on it and provision the plan you prefer.
Navigate to the Settings tab and click to reveal Config Vars. It should already have your DATABASE_URL. If not, go to the Overview tab and you should see your Heroku Postgres add-on, click on it to see all the information about your database. On the same page, go to the Settings tab to view your credentials. There you'll see your URI. Copy that and paste it as a value in your Config Vars with DATABASE_URL as the key. Note: Your key can be any valid name, just ensure it matches the name in your knex configuration. You can also add more config vars like the environment you wish to work with, i.e, production.
-
Review all your configurations. Make sure your knex configuration environment is not hardcoded to development. It should be similar to this:
const knex = require('knex'); const knexConfig = require('./knexfile'); const environment = process.env.DB_ENV || 'development'; module.exports = knex(knexConfig[environment]);
Your port number should also not be harcoded.
const port = process.env.PORT || 5000;
Of course, you can include the DB_ENV value in your Config Vars on
Heroku. -
Next, run the following command:
heroku run knex migrate:latest
And you're good to go. You can view the url of your deployed app by clicking on Open app on Heroku or scroll down to the domains section of the Settings tab.
Well done, champ. Now you can go grab that beer. Don't forget to let me know if you found this helpful, though.
Till next time......
Top comments (6)
Hey did all the steps included here, but for some reason I am getting "error: no pg_hba.conf entry for host" when I try to run my migration tables. Would you happen to know why?
And this is how my production connection looks like:
production: {
client: 'pg',
connection: process.env.DATABASE_URL,
migrations: {
directory: './data/migrations'
},
seeds: {
directory: './data/seeds'
},
}
I've seen this before when using postgres with knex. It was related to not using ssl as part of the connection config.
Thanks for breaking it down so flawlessy! Awesome article Sandrava 🤗
Hello there.
Thanks for the post.
Can you share the github link?
I'm still getting errors.
Hi Ighor, what errors are you getting exactly? You use this for reference: github.com/Sandravaphilips/dear-diary