【问题标题】:Sequelize gets stuck connecting to heroku postgres databaseSequelize 卡在连接到 heroku postgres 数据库
【发布时间】:2021-02-06 09:57:27
【问题描述】:

在 SO 上有很多这样的问题,但没有一个真正有同样的问题。

我正在尝试使用 sequelize 和 本地 连接到 Postgres 数据库,它与托管在 heroku 上的数据库一起使用。几天前它也可以工作,但现在不行了,查看差异并没有改变应用程序连接到数据库的方式。

当我部署应用程序时,它在尝试连接数据库时卡住了,直到 Web 工作人员因为应用程序未绑定到端口而引发超时。这使得调试变得困难,因为我没有要处理的错误。

  async connect(): Promise<void> {
    const dbUrl = process.env.DATABASE_URL as string

    if (!dbUrl) {
      throw new Error('Cannot connect to database, url not set!')
    }

    logger.info(`connecting to database ${dbUrl.replace(/:(\w+)@/, ':***@')}`)

    this.sequelize = new Sequelize(dbUrl, {
      dialect: 'postgres',
      protocol: 'postgres',
      logging: (sql, timing) => {
        logger.debug(`Executed SQL query '${sql}' in ${timing} ms`)
      },
      dialectOptions: {
        decimalNumbers: true,
        ssl: true
      }
    })
    
    // it gets stuck here
    await this.sequelize.authenticate({
      logging: (sql, timing) => {
        logger.debug(`authentication ${sql}, ${timing}`)
      }
    })
    logger.info('Database connection successful')

    logger.info('initializing models...')
    await this.initializeModels(this.sequelize)
    logger.info('synching database...')
    await this.sequelize.sync({
      alter: true,
      logging: (sql, timing) => {
        logger.debug(`Executed SQL query '${sql}' in ${timing} ms`)
      }
    })
  }

这里是日志输出:

ct 23 02:59:34 staging-monk-backend heroku/web.1 State changed from crashed to starting
Oct 23 02:59:40 staging-monk-backend heroku/web.1 Starting process with command `npm start`
Oct 23 02:59:43 staging-monk-backend app/web.1
Oct 23 02:59:43 staging-monk-backend app/web.1 > 8bit-bot-backend@1.0.0 start /app
Oct 23 02:59:43 staging-monk-backend app/web.1 > node build/main.js
Oct 23 02:59:43 staging-monk-backend app/web.1
Oct 23 02:59:44 staging-monk-backend app/web.1 2020-10-23T09:59:44.371Z [info] : NODE_ENV is production 
Oct 23 02:59:44 staging-monk-backend app/web.1 2020-10-23T09:59:44.375Z [info] : debug mode is off  
Oct 23 02:59:44 staging-monk-backend app/web.1 2020-10-23T09:59:44.375Z [info] : connecting to database postgres://rlbditxvnovvvq:***@ec2-54-246-87-132.eu-west-1.compute.amazonaws.com:5432/dd1v94insrdacc 
Oct 23 02:59:45 staging-monk-backend app/api Build succeeded
Oct 23 03:00:40 staging-monk-backend heroku/web.1 Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
Oct 23 03:00:40 staging-monk-backend heroku/web.1 Stopping process with SIGKILL
Oct 23 03:00:41 staging-monk-backend heroku/web.1 Process exited with status 137
Oct 23 03:00:41 staging-monk-backend heroku/web.1 State changed from starting to crashed

Procfile:

web:npm start

版本

  • pg: 7.5.0
  • sequelize:6.3.5

【问题讨论】:

    标签: node.js postgresql heroku sequelize.js


    【解决方案1】:

    issue 为我指明了正确的方向。问题是 pg 软件包版本,升级到 8.4.1 后一切正常。

    【讨论】:

    • 谢谢!我的问题只在我的代码没有更改后使用 Heroku 时出现。我猜他们一定升级了 postgres 版本。
    猜你喜欢
    • 2021-06-21
    • 2016-08-12
    • 2014-05-28
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2016-01-27
    • 2017-09-13
    • 2017-10-07
    相关资源
    最近更新 更多