简体   繁体   中英

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.

When I run the heroku bash CLI and attempt to migrate I get this error


    ~ $ 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


    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

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.

I am able to access the online database using psql in the command line. I have the correct tables and can create and retrieve data using SQL statements.

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.

I've tried changing my SSL to true which threw errors and to false which didn't seem to harm anything. (I've tried many other things)

If I hit the online heroku server it just throw a generic error without any details.

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.

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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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