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

Hello everyone! I think the title says it all.

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.

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

For your info, 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

Has anyone encountered this?

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

I did read this:

https://stackoverflow.com/questions/71322998/datagrip-08001-the-connection-attempt-failed-the-connection-attempt-failed 

But it does not help.

Any help is appreciated.

0
7 comments

Hi mostafa.zeinali ,

To better understand your incident, we need you to collect the logs from Help - Collect Logs and Diagnostic Data and upload it onto our FTP server:

https://uploads.jetbrains.com/

Provide the upload id. We'll have a look and get back to you.

0

Done:
Upload id: 2023_01_15_6szwKj3xrvXH1XiMCce7WJ (file: datagrip-logs-20230115-07412715925341540055077628.zip)

0

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 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:

https://superuser.com/a/1729731

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. 

 

 

0

So, now that this is solved, my only feedback is, a better error message could have solved this for me 2 weeks ago. The original exception was "SocketException", but the error message did not contain anything about socket. You know the people using your IDEs are "developers"! You don't need to protect me from the scary stack trace!

BTW, this is the original error message:

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

Thanks for your feedback.

The error mentioned in your earlier message is usually shown in the UI:

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

If you're looking for a more detailed error message, incl the stack trace, you can look into idea.log in Help - Show Log in Files.

When you're getting the following exception after making a connection attempt:

2023-01-14 12:14:19,273 [ 58076] INFO - #c.i.d.d.DatabaseCredentialsAuthProvider - Connecting as: postgres
2023-01-14 12:14:40,425 [ 79228] WARN - #c.i.d.d.BaseDatabaseErrorHandler$IOErrorInfo - Connection reset
java.net.SocketException: Connection reset

It's clearly indicating a connectivity issue. We do also have a troubleshooting guide for such incidents:

https://www.jetbrains.com/help/datagrip/connectivity-problems.html

But hey, glad you got it working.

0

Mostafa's solution helped me figure out why I could not connect to a database with Datagrip on Windows after I had already tried using the troubleshooting guide from here: https://www.jetbrains.com/help/datagrip/connectivity-problems.html

Maybe some parts of Mostafa's solution could be added to the troubleshooting guide? 

I could not connect to my database from Datagrip on Windows. It was working fine via JetBrains Gateway from a WSL PyCharm project. But it was not working from Datagrip running on Windows.

The parts that helped me find a solution (paraphrased) were:

If the connection is refused then there might be another process running on that port.

Shutdown the database and use netstat in Powershell to see if something else is running on the port (in my case 8123 for Clickhouse)

>> netstat -aon | findstr 8123

TCP    0.0.0.0:8123           0.0.0.0:0              LISTENING       4952

Then go into TaskManager>Details to see if another process is using that port

My solution was to use a different port for the database (I was port-forwarding from remote to localhost).

0

I'm happy this helped someone :3

0

Please sign in to leave a comment.