Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knex migration in postgres Heroku - Error: Unable to acquire connection

I am trying to run my first migration which creates a single table in a Heroku postgres database.

When I try to run knex migrate:latest --env development I receive the error

Error: Unable to acquire a connection

Things I've tried:

  • adding ?ssl=true to the end of my connection string stored in process.env.LISTINGS_DB_URL as I'm aware this is sometimes a requirement to connect with heroku
  • setting the env variable PGSSLMODE=require

I also stumbled across this article where someone has commented that knex will not accept keys based on environment. However, I'm attempting to follow along with this tutorial which indicates that it does. I've also seen numerous other references which re-enforce that.

I'll also add that I've been able to connect to the database from my application and from external clients. I'm only encountering this error when trying to run the knex migration.

Furthermore, I've tried identifying how I can check what is being sent as the connection string. While looking at the knex documentation:

How do I debug FAQ

If you pass {debug: true} as one of the options in your initialize settings, you can see all of the query calls being made.

Can someone help guide me in how I actually do this? Or have I already successfully done that in my knexfile.js?

Relevant files:

// knex.js:

var environment = process.env.NODE_ENV || 'development';
var config = require('../knexfile.js')[environment];

module.exports = require('knex')(config);



// knexfile.js:

module.exports = {

    development: {
        client: 'pg',
        connection: process.env.LISTINGS_DB_URL,
        migrations: {
            directory: __dirname + '/db/migrations'
        },
        seeds: {
            directory: __dirname + '/db/seeds'
        },
        debug: true
    },

    staging: {
        client: 'postgresql',
        connection: {
            database: 'my_db',
            user: 'username',
            password: 'password'
        },
        pool: {
            min: 2,
            max: 10
        },
        migrations: {
            tableName: 'knex_migrations'
        }
    },

    production: {
        client: 'postgresql',
        connection: {
            database: 'my_db',
            user: 'username',
            password: 'password'
        },
        pool: {
            min: 2,
            max: 10
        },
        migrations: {
            tableName: 'knex_migrations'
        }
    }

};
like image 709
HendPro12 Avatar asked Jan 13 '17 11:01

HendPro12


2 Answers

As noted by @hhoburg in comments below, the error Error: Unable to acquire a connectionis a generic message indicating something is incorrect with Knex client configuration. See here.

In my case, Knex wasn't referencing process.env.LISTINGS_DB_URL in knexfile.js because:

  • that variable was set in my .env file
  • the dotenv module wasn't be referenced/called by Knex

The correct way of setting this up is detailed in the knex issue tracker here.

like image 184
HendPro12 Avatar answered Sep 19 '22 01:09

HendPro12


I'm not sure if this will help at all, but I began running into the same issue today on my local environment. After way too much searching, I found that this is the new error message for an invalid connection configuration or a missing connection pool. After fiddling around with it for way too long, I switched my connection to use my .env file for the configuration environment; I had been using a hard-coded string ('dev') in my knex.js file, which didn't work for some reason.

Is your .env file working properly? Did you try messing with the pool settings, and are you positive your username and password are correct for the staging and production database?

I hope that link helps!

like image 39
hhoburg Avatar answered Sep 18 '22 01:09

hhoburg