我正在尝试运行我的第一次迁移,它在 Heroku postgres 数据库中创建一个表。
当我尝试运行 knex migrate:latest --env development
时,我收到错误
Error: Unable to acquire a connection
我尝试过的事情:
- 将
?ssl=true
添加到存储在process.env.LISTINGS_DB_URL
中的连接字符串的末尾,因为我知道这有时是连接 heroku 的要求 - 设置环境变量
PGSSLMODE=require
我也偶然发现了this article有人评论说 knex 不会接受基于环境的 key 。但是,我正在尝试跟随 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'
}
}
};
最佳答案
正如@hhoburg 在下面的评论中指出的那样,错误错误:无法获取连接
是一条通用消息,表明Knex client configuration 有问题。 .参见 here .
在我的例子中,Knex 没有在 knexfile.js 中引用 process.env.LISTINGS_DB_URL
,因为:
- 那个变量是在我的 .env 文件中设置的
- dotenv module未被 Knex 引用/调用
knex 问题跟踪器中详细介绍了正确的设置方法 here .
关于node.js - postgres Heroku 中的 Knex 迁移 - 错误 : Unable to acquire connection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41633344/