TypeORM无法连接到GCP Postgres实例

时间:2019-08-27 17:30:05

标签: google-cloud-functions google-cloud-sql nestjs typeorm

我有一个用NestJS编写的后端,必须使用GCP的数据库实例。在我的项目中,我使用了TypeORM并发现了这一已有两年历史的SO帖子,它应该对我有帮助:Typical ormconfig.json file for Google Cloud SQL?。我尝试了完全相同的操作,但是后端无法连接到SQL实例。

后端已部署到Firebase Functions,并且为

我在哪里加载TypeORM:

import {MiddlewareConsumer, Module, NestModule, RequestMethod} from "@nestjs/common";
import {AppController} from "./app.controller";
import {AppService} from "./app.service";
import {TypeOrmModule} from "@nestjs/typeorm";
import {UsersModule} from "./users/users.module";
import {AuthMiddleware} from "./middleware/auth.middleware";
import * as functions from "firebase-functions";

@Module({
    imports: [TypeOrmModule.forRoot(process.env.ENV === "PROD" ? {
            type: functions.config().production.postgres.type,
            host: functions.config().production.postgres.host,
            port: functions.config().production.postgres.port,
            extra: {
                socketPath: functions.config().production.postgres.extra,
            },
            username: functions.config().production.postgres.username,
            password: functions.config().production.postgres.password,
            database: functions.config().production.postgres.database,
            entities: functions.config().production.postgres.entities,
            synchronize: functions.config().production.postgres.synchronize,
        } : {
            type: "postgres",
            host: "localhost",
            port: 5432,
            username: "postgres",
            password: "postgres",
            database: "postgres",
            entities: ["src/**/*.entity{.ts,.js}"],
            synchronize: true,
        },
    ), UsersModule],
    controllers: [AppController],
    providers: [AppService],
})
export class AppModule implements NestModule {
    configure(consumer: MiddlewareConsumer) {
        consumer
            .apply(AuthMiddleware)
            .forRoutes({path: "users", method: RequestMethod.GET}, {path: "users", method: RequestMethod.PUT});
    }
}

我在尝试连接到SQL实例时从后端收到的错误:

Error: connect ETIMEDOUT XXX.XXX.XXX.XXX:5432 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1097:14)
[31m[Nest] 1   - [39m08/27/2019, 5:25 PM   [38;5;3m[TypeOrmModule] [39m[31mUnable to connect to the database. Retrying (2)...[39m[38;5;3m +67301ms[39m

涂黑的IP是SQL实例的IP。

我应该找什么?

0 个答案:

没有答案