Postgres Heroku中的Knex迁移-错误:无法获取连接 [英] Knex migration in postgres Heroku - Error: Unable to acquire connection

查看:143
本文介绍了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是一条通用消息,指示此处.

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迁移-错误:无法获取连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆