Knex迁移Postgres Heroku - 错误:无法获取连接 [英] Knex Migration Postgres Heroku - Error: Unable to Acquire Connection

查看:495
本文介绍了Knex迁移Postgres Heroku - 错误:无法获取连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图运行我的第一个迁移,在heroku postgres数据库中创建单个表。



当我尝试运行 knex migrate:latest --env development 时,收到错误错误:无法获取连接。



我试过的东西:




  • ?ssl = true 添加到存储在process.env.LISTINGS_DB_URL中的连接字符串的末尾,因为我知道这有时是需要与heroku连接
  • 设置env变量 PGSSLMODE = require



我也偶然发现了这篇文章 a>某人已经评论说knex不会接受基于环境的密钥。不过,我试图跟着本教程,它表明它的确如此。我还看到了许多其他的引用,这些引用重新强制执行。



我还会补充一点,我已经能够从我的应用程序和外部客户。我试图运行knex迁移时只遇到这个错误。



此外,我已经尝试确定如何检查连接字符串是如何发送的。在查看 knex文档时,我在如何调试常见问题解答部分中看到:如果您在初始化设置中将{debug:true}作为选项之一传递,您可以看到所有正在进行的查询调用。有人可以帮我指导我如何做到这一点?或者我已经在我的knexfile.js中成功完成了这项工作?



我的knex.js文件:

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

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

我的knexfile.js:

  module.exports = {

development:{
client:'pg',
connection:process.env.LISTINGS_DB_URL,
迁移:{
目录:__dirname +'/ db / migrations'
},
种子:{
目录:__dirname +'/ db / seeds'
},
debug:true
},
$ b $ staging:{
client:'postgresql',
connection:{
database:'my_db' ,
用户:'用户名',
密码:'密码'
},
池:{
最小值:2,
最大值:10
$,
迁移:{
tableName:'knex_migrations'
}
},

生成:{
client:'postgresql ',
连接:{
数据库:'my_db',
用户:'用户名',
密码:'密码'
},
池:{
分钟:2,
最多:10
},
迁移:{
tableName:'knex_migrations'
}
}

};


解决方案

正如@hhurg在下面的评论中指出的那样, 错误:无法获取连接是一条通用消息,指示 Knex客户端配置。请参阅此处



在我的情况下,Knex没有在knexfile.js中引用 process.env.LISTINGS_DB_URL ,因为:


  • 该变量已在我的.env文件中设置

  • dotenv模块未被Knex引用/调用



正确的设置方式详见knex问题跟踪器此处


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

};

解决方案

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.

这篇关于Knex迁移Postgres Heroku - 错误:无法获取连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆