【问题标题】:Knex Heroku server connection issues to postgres db - Node.jsKnex Heroku 服务器与 postgres db 的连接问题 - Node.js
【发布时间】:2020-10-11 01:36:42
【问题描述】:

我正在尝试使用 GraphQL 和 Knex 运行我的 node-express 服务器,并将其连接到 heroku 中的 PostgresQL 数据库。

当我运行 heroku bash CLI 并尝试迁移时,我收到此错误


    ~ $ npm run migrate

    > syncify-server@0.0.0 migrate /app
    > knex migrate:latest

    Using environment: staging
    Error: connect ECONNREFUSED 127.0.0.1:5432
        at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)
    npm ERR! code ELIFECYCLE
    npm ERR! errno 1
    npm ERR! syncify-server@0.0.0 migrate: `knex migrate:latest`
    npm ERR! Exit status 1
    npm ERR! 
    npm ERR! Failed at the syncify-server@0.0.0 migrate script.
    npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

    npm ERR! A complete log of this run can be found in:
    npm ERR!     /app/.npm/_logs/2020-06-21T00_20_17_455Z-debug.log

它在本地开发中运行良好。我的 knex.js 文件


    import dotenv from 'dotenv'
    import knex from 'knex'
    import mockKnex from 'mock-knex'

    dotenv.config()
    let knexConnection

    if (process.env.NODE_ENV === 'test') {
      knexConnection = knex({
        client: 'pg',
        debug: false,
      })
      mockKnex.mock(knexConnection)
     } else {
          knexConnection = knex({
            client: 'pg',
            connection: {
              url: process.env.DATABASE_URL,
              type: 'postgres',
              charset: 'utf8',
              ssl: false
            },
          })
        }

    export default knexConnection


和 knexfile.js

require('dotenv').config()

module.exports = {
  development: {
    client: 'pg',
    connection: {
      url: process.env.DATABASE_URL,
      charset: 'utf8',
    },
  },

  staging: {
    client: 'pg',
    connection: {
      url: process.env.DATABASE_URL,
      charset: 'utf8',
    },
    pool: {
      min: 2,
      max: 10,
    },
    migrations: {
      tableName: 'knex_migrations',
    },
  },

  production: {
    client: 'pg',
    connection: {
      url: process.env.DATABASE_URL,
      charset: 'utf8',
    },
    pool: {
      min: 2,
      max: 10,
    },
    migrations: {
      tableName: 'knex_migrations',
    },
  },
}

我一直试图找出问题所在,但感觉可能不止一个问题。

在配置变量中,DATABASE_URL 作为 heroku psql db URL,NODE_ENV 作为“暂存”以及所有 auth0 设置。

我可以在命令行中使用psql 访问在线数据库。我有正确的表,可以使用 SQL 语句创建和检索数据。

当我将本地服务器配置为使用 heroku psql db 时,我收到错误消息 relation "users" does not exist 如果我尝试数据库中的其他表也是如此。

我尝试将我的 SSL 更改为 true 会引发错误,而将其更改为 false 似乎不会造成任何损害。 (我尝试了很多其他的东西)

如果我点击在线 heroku 服务器,它只会抛出一个没有任何详细信息的通用错误。

源码here

【问题讨论】:

  • 尝试直接访问 API 会引发此错误:``` 错误:连接 ECONNREFUSED 127.0.0.1:80 ``` 这是授权标头设置为允许登录。如果不设置标题,它会正确返回用户需要授权

标签: node.js postgresql heroku psql


【解决方案1】:

在为此浪费了几天之后,我已经修复了它。

经典环境变量错误。 我没有在我的配置中包含AUTH0_ISSUER,这是在不久前的更新中实现的,但在第一次部署到heroku之后。

此外,尽管将我的 knex 文件配置为使用 DATABASE_URL,但它并没有拾取它,并且只有在我使用所有五个单独的设置时才连接到数据库。

DB_NAME=
DB_USER=
DB_PASSWORD=
DB_HOST=
DB_PORT=

我不知道为什么,但我在 './dist' 中有 server.js 文件,这些文件在缩小的代码中引用了这些文件,所以我猜它必须但不知何故已经转译以使用它,我有点卡住了。 宁愿使用DATABASE_URL,因为我认为heroku 会不时动态更新此值,并且不确定如何保护我的服务器免受此影响。 ?

【讨论】:

    猜你喜欢
    • 2019-07-15
    • 1970-01-01
    • 2020-07-14
    • 2014-11-24
    • 1970-01-01
    • 2018-08-14
    • 2011-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多