postgres Heroku 中的 Knex 迁移 - 错误:无法获取连接

2024-02-27

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

当我尝试跑步时knex migrate:latest --env development我收到错误

错误:无法获取连接

我尝试过的事情:

  • adding ?ssl=true到存储在我的连接字符串的末尾process.env.LISTINGS_DB_URL据我所知,有时这是连接 heroku 的要求
  • 设置环境变量PGSSLMODE=require

我也偶然发现本文 https://www.reddit.com/r/node/comments/54yisl/help_knex_with_sqlite3_unable_to_acquire/有人评论说 knex 不会根据环境接受密钥。但是,我正在尝试跟随本教程 http://mherman.org/blog/2016/04/28/test-driven-development-with-node/#.WHdPUhsrKUk这表明确实如此。我还看到了许多其他的参考资料,这些资料都强化了这一点。

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

此外,我尝试确定如何检查作为连接字符串发送的内容。在看着knex 文档 http://knexjs.org/#support:

如何调试常见问题解答

如果您将 {debug: true} 作为初始化中的选项之一传递 设置中,您可以看到所有正在进行的查询调用。

有人可以帮助指导我如何实际做到这一点吗?或者我已经在 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'
        }
    }

};

正如 @hhoburg 在下面的评论中指出的,错误Error: Unable to acquire a connection是一条通用消息,表明有问题Knex客户端配置 http://knexjs.org/#Installation-client. See here https://github.com/tgriesser/knex/issues/1583.

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

  • 该变量是在我的 .env 文件中设置的
  • the dotenv模块 https://www.npmjs.com/package/dotenv没有被 Knex 引用/调用

knex 问题跟踪器中详细介绍了正确的设置方法here https://github.com/tgriesser/knex/issues/590.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

postgres Heroku 中的 Knex 迁移 - 错误:无法获取连接 的相关文章

随机推荐