简体   繁体   English

Knex Heroku 服务器与 postgres 数据库的连接问题 - Node.js

[英]Knex Heroku server connection issues to postgres db - Node.js

I am trying to run my node-express server with GraphQL and Knex and connect it up to the PostgresQL database in heroku.我正在尝试使用 GraphQL 和 Knex 运行我的 node-express 服务器,并将其连接到 heroku 中的 PostgresQL 数据库。

When I run the heroku bash CLI and attempt to migrate I get this error当我运行 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

It is working fine locally in development.它在本地开发中运行良好。 My knex.js file我的 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


and knexfile.js和 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',
    },
  },
}

I've been trying to isolate the problem but feel like I might have more than one.我一直在试图找出问题所在,但感觉可能不止一个。

In config vars I have the DATABASE_URL as the heroku psql db URL and NODE_ENV as "staging" as well as all the auth0 settings.在配置变量中,我将DATABASE_URL作为 heroku psql db URL 和NODE_ENV作为“暂存”以及所有 auth0 设置。

I am able to access the online database using psql in the command line.我可以在命令行中使用psql访问在线数据库。 I have the correct tables and can create and retrieve data using SQL statements.我有正确的表,可以使用 SQL 语句创建和检索数据。

When I configure my local server to use the heroku psql db I get the error message relation "users" does not exist The same if I try other tables in the database.当我将本地服务器配置为使用 heroku psql db 时,如果我尝试数据库中的其他表,我会收到错误消息relation "users" does not exist

I've tried changing my SSL to true which threw errors and to false which didn't seem to harm anything.我已经尝试将我的 SSL 更改为 true ,这会引发错误,而更改为 false 似乎没有任何伤害。 (I've tried many other things) (我尝试了很多其他的东西)

If I hit the online heroku server it just throw a generic error without any details.如果我点击在线 heroku 服务器,它只会抛出一个没有任何细节的通用错误。

Source code here源代码在这里

After losing days to this I've fixed it.在为此失去几天之后,我已经修复了它。

Classic env variable errors.经典环境变量错误。 I hadn't included AUTH0_ISSUER in my config which was implemented in an update a while ago but after the first deployment to heroku.我没有在我的配置中包含AUTH0_ISSUER ,这是在前一段时间的更新中实现的,但是在第一次部署到 heroku 之后。

Also despite configuring my knex files to use DATABASE_URL it was not picking it up and only connects to the database if I used all five individiual settings.此外,尽管将我的 knex 文件配置为使用DATABASE_URL ,但它并没有拾取它,并且只有在我使用所有五个单独设置时才连接到数据库。

DB_NAME=
DB_USER=
DB_PASSWORD=
DB_HOST=
DB_PORT=

I couldn't work out why exactly but I have server.js files in './dist' which have these referenced in minified code so I guess it must but somehow transpiled already to use that and I a bit stuck.我不知道为什么,但是我在'./dist'中有server.js文件,这些文件在缩小的代码中引用了这些文件,所以我猜它必须但不知何故已经转译以使用它,我有点卡住了。 Would prefer to use DATABASE_URL as I think heroku dynamically updates this value from time to time and not sure how to protect my server from that.更喜欢使用DATABASE_URL因为我认为 heroku 会不时动态更新此值,并且不确定如何保护我的服务器免受此影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM