如何修复 NestJS 中 Heroku 的错误“没有主机的 pg_hba.conf 条目”

问题描述 投票:0回答:2

我使用指南为 Heroku 配置了 TypeORM,

ormconfig.js
看起来像这样:

const dbConfig = {
    synchronize: false,
    migrations: ['migrations/*.js'],
    cli: {
      migrationsDir: 'migrations',
    },
};

switch(process.env.NODE_ENV){
    case 'development':
        Object.assign(dbConfig, {
            type: 'sqlite',
            database: 'db.sqlite',
            entities: ['**/*.entity.js'],
        });
        break;
    case 'test':
        Object.assign(dbConfig, {
            type: 'sqlite',
            database: 'test.sqlite',
            entities: ['**/*.entity.ts'],
            migrationsRun: true,
            ssl: {
                rejectUnauthorized: false
            }
        });
        break;
    case 'production':
        Object.assign(dbConfig, {
            type: 'postgres',
            url: process.env.DATABASE_URL,
            migrationsRun: true,
            entities: ['**/*.entity.js'],
        });
        break;
    default:
         throw new Error('unknown environment')
    
}

module.exports = dbConfig;

我还在 Heroku 的项目中使用命令

Procfile
创建了一个
web: npm run start:prod
文件。 我目前只有一些简单的路线,在本地我可以使用 SQLite 运行 api,不会出现错误。但是当我尝试在 Heroku 上运行 api 时,出现此错误:

2022-07-17T11:53:53.748981+00:00 app[web.1]: error: no pg_hba.conf entry for host "xx.xxx.xxx.xxx", user "foofoofoof", database "foofoofoof", no encryption
2022-07-17T11:53:53.748981+00:00 app[web.1]:     at Parser.parseErrorMessage (/app/node_modules/pg-protocol/dist/parser.js:287:98)
2022-07-17T11:53:53.748982+00:00 app[web.1]:     at Parser.handlePacket (/app/node_modules/pg-protocol/dist/parser.js:126:29)
2022-07-17T11:53:53.748982+00:00 app[web.1]:     at Parser.parse (/app/node_modules/pg-protocol/dist/parser.js:39:38)
2022-07-17T11:53:53.748983+00:00 app[web.1]:     at Socket.<anonymous> (/app/node_modules/pg-protocol/dist/index.js:11:42)
2022-07-17T11:53:53.748983+00:00 app[web.1]:     at Socket.emit (node:events:527:28)
2022-07-17T11:53:53.748984+00:00 app[web.1]:     at addChunk (node:internal/streams/readable:315:12)
2022-07-17T11:53:53.748984+00:00 app[web.1]:     at readableAddChunk (node:internal/streams/readable:289:9)
2022-07-17T11:53:53.748984+00:00 app[web.1]:     at Socket.Readable.push (node:internal/streams/readable:228:10)
2022-07-17T11:53:53.748984+00:00 app[web.1]:     at TCP.onStreamRead (node:internal/stream_base_commons:190:23)
2022-07-17T11:53:53.879579+00:00 heroku[web.1]: Process exited with status 1
2022-07-17T11:53:53.945790+00:00 heroku[web.1]: State changed from starting to crashed

我使用与我的数据库相同的指令配置了我的

app.module
,它看起来像这样:

import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { IngredientsModule } from './ingredients/ingredients.module';
import { RecipesModule } from './recipes/recipes.module';
import { UsersModule } from './users/users.module';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      envFilePath: `.env.${process.env.NODE_ENV}`,
    }),
    TypeOrmModule.forRoot(),
    UsersModule,
    IngredientsModule,
    RecipesModule,
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

对于 Heroku,我在 Config Vars 下创建了三个环境变量

COOKIE_KEY
DATABASE_URL
(自动创建)、
NODE_ENV
enter image description here

我的脚本部分在

package.json
看起来像这样:

  "scripts": {
    "prebuild": "rimraf dist",
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "cross-env NODE_ENV=development nest start",
    "start:dev": "cross-env NODE_ENV=development nest start --watch",
    "start:debug": "cross-env NODE_ENV=development nest start --debug --watch",
    "start:prod": "node dist/main",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "cross-env NODE_ENV=test jest",
    "test:watch": "cross-env NODE_ENV=test jest --watch --maxWorkers=1",
    "test:cov": "cross-env NODE_ENV=test jest --coverage",
    "test:debug": "cross-env NODE_ENV=test node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "cross-env NODE_ENV=test jest --config ./test/jest-e2e.json --maxWorkers=1",
    "typeorm": "cross-env NODE_ENV=development node --require ts-node/register ./node_modules/typeorm/cli.js"
  },

如何修复 NestJS 或 Heroku 中的此错误?

nestjs typeorm heroku-postgres nestjs-config
2个回答
4
投票

我找到了如何消除此错误的解决方案。

在控制台中运行:

heroku config:set PGSSLMODE=no-verify

0
投票

我遇到了同样的问题,通过添加解决了

new DataSource({
    type: 'postgres',
    ssl: { rejectUnauthorized: false },
    ...,
  })

TypeORM 的 DataSource 接口与

ormconfig.json
略有不同,后者似乎更常见。

https://stackoverflow.com/a/65752601/2339067

© www.soinside.com 2019 - 2024. All rights reserved.