You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用TypeOrm连接NestJS到PostgreSQL时遇TypeError问题求助

解决TypeORM连接PostgreSQL时的this.postgres.Pool is not a constructor错误

问题原因

这个错误是TypeORM 0.3.x版本与pg 8.x版本的兼容性配置问题,加上配置项里多余的driver字段共同导致的:

  • TypeORM 0.3.x已经废弃driver配置参数,保留该字段会干扰内部PostgreSQL连接池的初始化逻辑;
  • pg 8.x模块导出结构有变化,错误的配置会导致TypeORM无法正确引用Pool构造函数。

解决方案

1. 移除配置中的driver字段

修改TypeOrmModule配置,删除多余的"driver": "postgres"项,仅保留type: "postgres"即可:

@Module({
  imports: [TypeOrmModule.forRoot({
    "type": "postgres",
    "host": dbHost,
    "port": 5432,
    "database": dbName,
    "username": dbUser,
    "password": dbPassword,
    "synchronize": true,
    "logging": true,
    "entities": [
      "src/**/entity/*.ts",
      "dist/**/entity/*.js"
    ]
  })],
})

2. 优化实体路径配置(可选)

为避免编译后实体路径找不到的问题,可改用更可靠的相对路径写法:

"entities": [
  __dirname + '/**/*.entity{.ts,.js}'
]

3. 清理并重新安装依赖

如果上述修改后仍有问题,可能是依赖缓存导致的版本不兼容,执行以下命令:

rm -rf node_modules package-lock.json
npm install

内容的提问来源于stack exchange,提问作者Mustafa İlker

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 22:30:51