NestJS无法连接PostgreSQL数据库问题求助
PostgreSQL数据库连接失败问题排查
问题描述
开发Quiz API时遇到PostgreSQL数据库连接失败,报错Error: Connection terminated unexpectedly。应用在添加实体前可正常运行,移除实体后仍无法连接,疑问是否存在连接本身之外的原因。
相关配置
app.module配置
@Module({ imports: [ GraphQLModule.forRoot<ApolloDriverConfig>({ driver: ApolloDriver, autoSchemaFile: './schema.gql', playground: true, }), TypeOrmModule.forRoot({ keepConnectionAlive: true, type: 'postgres', port: 5433, username: 'postgres', password: '12345', database: 'postgres', autoLoadEntities: true, synchronize: false, }), QuizModule, QuestionModule, AnswerModule ], controllers: [AppController], providers: [AppService], }) export class AppModule {}
docker-compose配置
version: "3.9" services: postgres: image: postgres:13-alpine restart: always env_file: - .env environment: - POSTGRES_USER=$POSTGRES_USER - POSTGRES_PASSWORD:$POSTGRES_PASSWORD ports: - "$POSTGRES_PORT:$POSTGRES_PORT_DOCKER" volumes: - postgres_data:/var/lib/postgresql/data networks: - db_network volumes: postgres_data: networks: db_network:
.env配置
POSTGRES_USER=postgres POSTGRES_URL=postgresql://postgres:12345@localhost:5432/postgres?schema=public POSTGRES_PASSWORD=12345 POSTGRES_PORT_DOCKER=5433 POSTGRES_PORT=5433
报错信息
[Nest] 34393 - 07/10/2023, 10:16:39 PM LOG [NestFactory] Starting Nest application... [Nest] 34393 - 07/10/2023, 10:16:39 PM LOG [InstanceLoader] TypeOrmModule dependencies initialized +24ms [Nest] 34393 - 07/10/2023, 10:16:39 PM LOG [InstanceLoader] QuizModule dependencies initialized +1ms [Nest] 34393 - 07/10/2023, 10:16:39 PM LOG [InstanceLoader] QuestionModule dependencies initialized +0ms [Nest] 34393 - 07/10/2023, 10:16:39 PM LOG [InstanceLoader] AnswerModule dependencies initialized +0ms [Nest] 34393 - 07/10/2023, 10:16:39 PM LOG [InstanceLoader] AppModule dependencies initialized +0ms [Nest] 34393 - 07/10/2023, 10:16:39 PM LOG [InstanceLoader] GraphQLSchemaBuilderModule dependencies initialized +0ms [Nest] 34393 - 07/10/2023, 10:16:39 PM LOG [InstanceLoader] GraphQLModule dependencies initialized +0ms [Nest] 34393 - 07/10/2023, 10:16:39 PM ERROR [TypeOrmModule] Unable to connect to the database. Retrying (1)... Error: Connection terminated unexpectedly
问题分析与解决方案
你的问题大概率还是数据库连接配置本身的问题,而非实体相关因素,以下是具体排查点和修复步骤:
1. 容器内部端口错误
PostgreSQL默认容器内部端口是5432,但你的.env中POSTGRES_PORT_DOCKER=5433,这会导致容器尝试在内部用5433启动PostgreSQL,但镜像默认监听5432,端口不匹配直接导致连接失败。
修复:
修改.env:
POSTGRES_PORT_DOCKER=5432
2. 配置端口统一冲突
当前.env中的POSTGRES_URL用了5432端口,而TypeORM配置的是5433,两者端口不一致会导致连接逻辑混乱。
修复:
统一端口配置,保持外部端口5433映射到容器内部5432:
- 更新
POSTGRES_URL为:POSTGRES_URL=postgresql://postgres:12345@localhost:5433/postgres?schema=public - TypeORM的
port:5433配置保持不变(对应外部映射端口)
3. Docker数据卷异常
如果之前的容器初始化出现问题,数据卷可能残留异常状态,导致PostgreSQL服务启动失败。
修复:
删除数据卷并重新初始化容器:
docker-compose down -v docker-compose up -d
4. 网络访问问题
如果应用是本地运行(非Docker容器),需确保通过localhost+映射端口能访问到Docker内的PostgreSQL;如果应用也在Docker中,需将应用加入db_network网络,并使用服务名postgres作为主机名而非localhost。
关于实体的疑问
移除实体后仍无法连接,说明实体不是问题根源。之前添加实体前能运行,可能是当时的配置刚好正确,后续修改配置(比如端口)才导致连接失败,和实体无关。
内容的提问来源于stack exchange,提问作者Adrian Cielniak
相关产品推荐
相关产品推荐

