Reputation: 93
A wired problem. I wrote a scala program, which would connect to a local postgresql database. This program ran fine weeks ago, but when I run it today, it throws exception:
Caused by: java.net.UnknownHostException: localhost
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at org.postgresql.core.PGStream.<init>(PGStream.java:68)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:144)
I change the connection url to "127.0.0.1", it throws that
Caused by: java.net.UnknownHostException: 127.0.0.1
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at org.postgresql.core.PGStream.<init>(PGStream.java:68)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:144)
... 16 more
In these two situations, the connection urls are:
jdbc:postgresql://localhost:5432/readinglist
jdbc:postgresql://127.0.0.1:5432/readinglist
Additional info may useful:
I can connect to the local postgresql database with "--host=localhost" option in shell on my computer
my computer is mac book air and no vm or docker is running
change connection url to a remote database and it runs fine
I run the program in IDEA as well as in the shell, both are failed to connect to the local database
rebooting computer not working
add or comment "127.0.0.1 localhost" in /etc/hosts and then rebooting not working
More info can be afford if you want.
So I want to know what will make a program can't know "localhost" or "127.0.0.1", or without knowing the real cause, what else can I do to fix it?
Upvotes: 1
Views: 1289
Reputation: 1
It sounds like a similar problem I just solved. It turns out that it's the ShadowsocksX-R blocking the connection between PostgreSQL and scala program. You can see if you have ShadowsocksX-R or any VPN is on.
Here is the ShadowsocksX-R version which caused problem on my Mac: screenshot of ShadowsocksX-R version
Upvotes: 0
Reputation: 558
Did you happen to add a SOCKS proxy since it last worked? Look at your stack trace:
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
The JVM is using a SOCKS proxy and that proxy apparently can't resolve localhost. Try removing the SOCKS proxy from your network config. In my experience Java is a little flakey with SOCKS.
Upvotes: 4