Knex Migration Postgres Heroku - 错误:无法获取连接

Hen*_*o12 3 heroku database-migration node.js heroku-postgres knex.js

我正在尝试运行我的第一个迁移,它在heroku postgres数据库中创建一个表.

当我尝试运行时,knex migrate:latest --env development我收到错误Error: Unable to acquire a connection.

我试过的事情:

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

我也偶然发现了这篇文章,有人评论说knex不会接受基于环境的密钥.但是,我正在尝试按照本教程进行操作,这表明它确实如此.我还看到了许多其他引用来强化它.

我还要补充一点,我已经能够从我的应用程序和外部客户端连接到数据库.我在尝试运行knex迁移时遇到此错误.

此外,我已经尝试确定如何检查作为连接字符串发送的内容.在查看knex文档时,我会在"如何调试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.有人可以帮我指导我的实际操作吗?或者我已经在我的knexfile.js中成功完成了这项工作?

我的knex.js文件:

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

module.exports = require('knex')(config);
Run Code Online (Sandbox Code Playgroud)

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

};
Run Code Online (Sandbox Code Playgroud)

Hen*_*o12 5

正如@hhoburg在下面的评论中所指出的,错误Error: Unable to acquire a connection是一个通用消息,表明Knex客户端配置有些不正确.看到这里.

就我而言,Knex没有引用process.env.LISTINGS_DB_URLknexfile.js,因为:

  • 该变量是在我的.env文件中设置的
  • dotenv模块并没有被引用/被叫由Knex

设置此问题的正确方法在此处的knex问题跟踪器中有详细说明.