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

Windows 11环境下无法连接本地Docker部署的PostgreSQL数据库的替代连接方案咨询

Fixing PostgreSQL Docker Connection Issues on Windows 11

Let's break down why you're hitting this connection error and walk through the fixes step by step.

Root Cause 1: Incorrect Port Mapping in docker-compose.yml

Your current ports: - "5434" configuration doesn’t create a fixed port mapping. Docker will randomly assign a host port to the container’s default PostgreSQL port (5432) instead of binding it to 5434. That’s why localhost:5434 can’t reach the database.

Fix Step 1: Update docker-compose.yml for Fixed Port Mapping

Modify the ports section to explicitly map your host port 5434 to the container’s 5432 port:

version: '3.8'
services:
  dev-db :
    image : postgres:13
    ports :
      - "5434:5432"  # Host port : Container port (PostgreSQL defaults to 5432)
    environment :
      POSTGRES_USER : postgres
      POSTGRES_PASSWORD : eresto
      POSTGRES_DB : eresto
    networks :
      - 4s
networks:
  4s:

Fix Step 2: Restart the Container

Apply the changes by stopping and restarting your Docker service:

docker-compose down
docker-compose up -d

Verify the Port Mapping

Run docker ps and check the PORTS column for your dev-db container. You should see something like 0.0.0.0:5434->5432/tcp—this confirms the port is correctly bound.

If Connection Still Fails: Address Windows 11/WSL2 Specific Issues

If you’re using Docker with the WSL2 backend (the default for Windows 11), there are a few extra quirks to address:

Option 1: Use WSL2's IP Address Instead of localhost

Sometimes localhost doesn’t properly route to WSL2 containers. To get your WSL2 IP:

  1. Open a WSL terminal (e.g., Ubuntu)
  2. Run hostname -I to get the active IP address
  3. Update your Prisma .env file's DATABASE_URL to use this IP instead of localhost:
    DATABASE_URL="postgresql://postgres:eresto@<WSL2_IP>:5434/eresto?schema=public"
    

Option 2: Use Host Network Mode

You can run the container directly on your host's network (bypassing Docker’s network isolation) by adding network_mode: host to your docker-compose.yml (remove the ports section since it’s no longer needed):

version: '3.8'
services:
  dev-db :
    image : postgres:13
    network_mode: host
    environment :
      POSTGRES_USER : postgres
      POSTGRES_PASSWORD : eresto
      POSTGRES_DB : eresto

Then connect using localhost:5432 (since the container uses the host network and PostgreSQL defaults to 5432). Note: Ensure no other PostgreSQL instance is running on your host's 5432 port.

Option 3: Check Windows Firewall Rules

Windows Firewall might be blocking inbound traffic to port 5434. Add an inbound rule to allow TCP traffic on port 5434:

  1. Open Windows Defender Firewall > Advanced Settings
  2. Create a new Inbound Rule > Port > TCP > Specific local ports: 5434
  3. Allow the connection > Apply to all network types > Name the rule and save

Final Check: Verify Prisma Configuration

Double-check your .env file's DATABASE_URL matches the credentials and port from your docker-compose.yml:

DATABASE_URL="postgresql://postgres:eresto@localhost:5434/eresto?schema=public"

Then re-run npx prisma migrate dev—it should connect successfully now.

内容的提问来源于stack exchange,提问作者Srikar Marupaka

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 14:47:35