Knex connection bug with Heroku Postgres.

I was getting a weird error when attempting to build a schema in a Heroku Postgres instance.

Take Knex’s docs and Herokus auto generated connection string and you’ll get something like the following.

var knex = require('knex')({
    client: 'pg',
    connection: 'postgres://myUser:myPassword@myHost:5432/myDb'
});

Then you get a weird error; related to SSL. The suggested fix from some reading is adding ?ssl=true to the end of the url.

var knex = require('knex')({
    client: 'pg',
    connection: 'postgres://myUser:myPassword@myHost:5432/myDb?ssl=true'
});

Still doesn’t work! However….throw it in a connection object….

var knex = require('knex')({
    client: 'pg',
    connection: {
        host: 'myHost',
        user: 'myUser',
        password: 'myPassword',
        database: 'myDb',
        ssl: true
    }
});

And bam. It all works.