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

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

我正在尝试 运行 我的第一次迁移,它在 Heroku postgres 数据库中创建一个 table。

当我尝试 运行 knex migrate:latest --env development 我收到错误

Error: Unable to acquire a connection

我尝试过的事情:

我也偶然发现了 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,这表明确实如此。我还看到了许多其他的参考资料,这些参考资料再次强调了这一点。

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

此外,我尝试确定如何检查作为连接字符串发送的内容。在查看 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.

有人可以帮助指导我如何实际执行此操作吗?或者我已经在我的 knexfile.js 中成功完成了吗?

相关文件:

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

};

我不确定这是否有帮助,但我今天在本地环境中开始 运行 遇到同样的问题。经过大量搜索后,我发现 this is the new error message 表示连接配置无效或缺少连接池。在摆弄它太久之后,我切换了我的连接以使用我的 .env 文件作为配置环境;我一直在我的 knex.js 文件中使用 hard-coded 字符串 ('dev'),但由于某种原因它不起作用。

您的 .env 文件是否正常工作?您是否尝试过修改池设置,您确定您的用户名和密码对于登台和生产数据库是正确的吗?

希望link对您有所帮助!

正如@hhoburg 在下面的评论中指出的那样,错误 Error: Unable to acquire a connection 是一条通用消息,表明 Knex client configuration. See here.

有问题

就我而言,Knex 没有在 knexfile.js 中引用 process.env.LISTINGS_DB_URL,因为:

  • 那个变量是在我的 .env 文件中设置的
  • dotenv module 不是 referenced/called Knex

knex 问题跟踪器中详细介绍了正确的设置方法 here

第 1 步:

首先安装dotenv:

npm i dotenv --save

在项目根目录下创建一个.env文件,添加:

DATABASE_URL=postgres://...

第 2 步:

knexfile.js 的开头添加:

require('dotenv').config();

将 postgres 连接更改为:

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

我在同样的情况下收到了同样的错误。原来我忘记在迁移之前提供数据库,所以没有什么可以连接的。

要修复此错误,

在 运行 之前:

heroku run knex migrate:latest

我运行这个命令:

heroku addons:create heroku-postgresql

效果很好。

如果您在 nodejs 中遇到此错误,请尝试删除此行

myDb.destroy().then();

在 运行 相应迁移之前尝试将数据更新到数据库时出现此错误。