Postgres Heroku中的Knex迁移

编程入门 行业动态 更新时间:2024-10-26 18:18:00
本文介绍了Postgres Heroku中的Knex迁移-错误:无法获取连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I am trying to run my first migration which creates a single table in a Heroku postgres database.

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

When I try to run knex migrate:latest --env development I receive the error

错误:无法获取连接

Error: Unable to acquire a connection

我尝试过的事情:

  • 将?ssl=true添加到存储在process.env.LISTINGS_DB_URL中的连接字符串的末尾,因为我知道有时需要与heroku连接
  • 设置环境变量PGSSLMODE=require
  • 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

我还偶然发现了这篇文章,其中有人对此发表了评论.该knex将不接受基于环境的密钥.但是,我尝试遵循本教程表明确实如此.我还看到了许多其他参考文献来加强这一点.

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.

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

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.

此外,我尝试确定如何检查作为连接字符串发送的内容.在查看 knex文档:

Furthermore, I've tried identifying how I can check what is being sent as the connection string. While looking at the knex documentation:

如何调试常见问题解答

如果您通过{debug:true}作为初始化中的选项之一 设置,您可以查看所有正在执行的查询.

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中成功完成了此任务?

Can someone help guide me in how I actually do this? Or have I already successfully done that in my 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客户端配置.请参见此处.

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.

就我而言,Knex在knexfile.js中未引用process.env.LISTINGS_DB_URL,原因是:

In my case, Knex wasn't referencing process.env.LISTINGS_DB_URL in knexfile.js because:

  • 该变量已在我的.env文件中设置
  • Knex未引用/调用 dotenv模块.

在knex问题跟踪器此处中详细介绍了设置此方法的正确方法.

The correct way of setting this up is detailed in the knex issue tracker here.

更多推荐

Postgres Heroku中的Knex迁移

本文发布于:2023-10-16 05:17:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1496611.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Postgres   Heroku   Knex

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!