Knex Migration Postgres Heroku - Error: Unable to Acquire Connection

时间秒杀一切 提交于 2019-12-10 10:18:16

问题


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, I see under the "How do I debug FAQ section:" 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?

My knex.js file:

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

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

My 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'
        }
    }

};

回答1:


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.




回答2:


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!




回答3:


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: ...
  }
}



回答4:


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.



来源:https://stackoverflow.com/questions/41633344/knex-migration-postgres-heroku-error-unable-to-acquire-connection

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