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

求助:无法通过Nhipster生成代码连接PostgreSQL数据库

问题分析与解决方案

问题根源

你的Nhipster生成应用中,orm.config.ts的数据库配置存在明显矛盾:

  • 数据库类型指定为postgres,但驱动却加载了sqlite3,导致TypeORM尝试用SQLite驱动初始化PostgreSQL连接池,触发this.postgres.Pool is not a constructor错误。

修复步骤

  1. 修正驱动配置
    修改server\src\orm.config.ts中的dev环境配置,将错误的SQLite驱动替换为PostgreSQL对应驱动,或者直接删除driver字段(TypeORM会自动根据type加载对应驱动):

    else if (process.env.BACKEND_ENV === 'dev') {
        ormconfig = {
          name: 'default',
          type: 'postgres',
          // 移除错误的sqlite3驱动,或替换为pg驱动
          // driver: require('pg'),
          database: 'judiciary',
          host: '127.0.0.1',
          port: 5432,
          username: 'postgres',
          password: 'root321',
          logging: true,
        };
      } 
    
  2. 确保安装PostgreSQL驱动依赖
    检查项目是否已安装pg包,若未安装,执行以下命令:

    npm install pg
    # 或使用yarn
    yarn add pg
    
  3. 重启应用
    重新启动后端服务,验证数据库连接是否正常。

验证逻辑

你手动创建的NestJS应用未指定driver字段,TypeORM自动加载了PostgreSQL的pg驱动,因此连接正常。只需对齐Nhipster生成的配置即可解决问题。

内容的提问来源于stack exchange,提问作者Madasu K

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 23:28:17