简体   繁体   中英

DataGrip Cannot Connect to Local PostgreSQL Running in Docker Desktop for Windows with WSL2 Backend

I have Docker Desktop installed on windows 10. It uses WSL2 back-end. I have 3 databases running on docker. One Mongo, One Clickhouse, and one PostgreSQL. DataGrip can easily connect to the Clickhouse on localhost:8123, and also to the Mongo on localhost:27017 but for some reason it cannot connect to the PostgreSQL running on 5432.

The peculiar thing about this is that pgAdmin can connect to the PostgreSQL on localhost:5432.

DataGrip can easily connect to the two other containers in this docker-compose file.

This is my docker compose, which I use to run the three containers:

version: "3.9"
services:
  postgres:
    image: postgres:15.1-alpine
    restart: on-failure
    ports:
      - "5432:5432"
    volumes:
      - fpm_pg:/var/lib/postgresql/data
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_DB=arm
      - POSTGRES_PASSWORD=postgres

  mongo:
    image: "mongo:latest"
    ports:
      - "27017:27017"
    env_file:
      - .env
    restart: "no"

  clickhouse:
    image: "clickhouse/clickhouse-server"
    ports:
      - "8123:8123"
      - "9000:9000"
      - "9004:9004"
    depends_on:
      - postgres
    env_file:
      - .env
    restart: "no"

volumes:
  fpm_pg:
    driver: local

Error:

DBMS:  Case sensitivity: plain=mixed,
delimited=exact 
Driver:  (ver. , JDBC) 
Effective version: PostgreSQL (ver. 0.0) 
The connection attempt failed.

Has anyone encountered this?

I also cannot establish this connection from within "Goland", which was the first thing I tried.

I did read this: DataGrip [08001] The connection attempt failed. The connection attempt failed , but it does not help.

OK. I solved it. After reading the log file myself, I noticed that it's a socket exception which led me to believe that there's something wrong with the port 5432. I still maintain that because pgAdmin could connect to that DB, DataGrip should have been able to do the same, but let's go down the rabbit hole shall we? I used this video to inspect my ports:

How to find which application is using your TCP ports

I inspected the ports using:

netstat -an | findstr 5432

which showed me that even when the DB container is not running and even Docker itself is closed, something is listening on 5432.

I used:

netstat -aon | findstr 5432

I found out that a PID:4846 is running on 5432. Going into TaskManager>Details, finding PID 4846 led me to find out that "Windows IP Helper service" is listening on this port. A quick search led me to this answer:

Windows IP Helper Service (iphlpsvc) - is it possible to change port?

And I also remembered that for a previous project on an older version of docker in WSL2 which had a lot of problems forwarding ports, I had used port forwarding on this port. So, a quick:

netsh interface portproxy show all
netsh interface portproxy delete v4tov4 listenport=5432 listenaddress=0.0.0.0
netsh interface portproxy delete v4tov4 listenport=5432 listenaddress=127.0.0.1

solved the issue.

Now, I should again stress, that for some reason, pgAdmin didn't ahve any problem with the port forwarding and could connect to the DB with no problem, which led me to believe the port and connection should be fine. Hope this helps someone in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM