Knex Migration Postgres Heroku - Error: Unable to Acquire Connection

回眸只為那壹抹淺笑 提交于 2019-12-05 19:59:59

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.

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!

Step 1:

First install dotenv:

npm i dotenv --save

Create a .env file in the root of your project, add:

DATABASE_URL=postgres://...

Step 2:

In the beginning of your knexfile.js, add:

require('dotenv').config();

Change the postgres connection to something like:

{
  client: 'postgresql',
  connection: process.env.DATABASE_URL,
  pool: {
    min: 0,
    max: 15
  },
  migrations: {
    directory: ...
  },
  seeds: {
    directory: ...
  }
}
undefinedindustries

I received this same error in the same situation. Turns out I forgot to provision a database before migrating, so there was nothing to connect to.

To fix this error,

Before running:

heroku run knex migrate:latest

I ran this command:

heroku addons:create heroku-postgresql

and that worked nicely.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!