I recently deployed a simple Express server to Heroku. The project used Knex.js for SQL queries and database migrations.
While deploying, I ran into some issues with my knexfile. That is, I was able to create the database using the Heroku CLI, but running the migrations and configuring the database connection took a bit of finessing.
Long story short, two parts:
1. Database URL (Connection)
Find your database url. Using Heroku CLI, running heroku config
(or heroku config --app [app name]
) will return something like the following:
1 2 3 |
|
Copy and paste the DATABASE_URL as your Knexfile’s connection
value.
2. SSL
Heroku requires SSL for PostgreSQL connections. There are two options (and potentially a third in the future):
- (Confirmed) Add an
ssl: true
key/value pair to the Knexfile - (Confirmed) Set an environment variable using Heroku CLI:
heroku config:set PGSSLMODE=require
- (Unsupported, but may be implemented in future versions of Knex) Add a
'?ssl=true'
query parameter to your database URL (knexfile’sconnection
).
With those two things in mind, a Knexfile like the following will work just fine:
1 2 3 4 5 6 7 8 9 |
|