英文:
Can't connect to local Postgres in IntelliJ IDEA
问题
以下是您要翻译的内容:
这似乎应该是一个非常基本的问题,但它让我快要发疯了,浪费了我几天的工作时间。
我正在尝试在本地运行Java集成测试,但由于PSQLException而失败。在我的终端中,我可以正常ping localhost
,并且我可以成功telnet到5432端口。我还可以使用Postico成功访问本地数据库。为了解决这个问题,我正在尝试在IDEA中配置一个数据库。这相当基础。URL是jdbc:postgresql://localhost:5432/postgres
,所有其他设置都与我成功的Postico配置匹配。在“驱动程序”选项卡中,我没有更改任何内容;PostgreSQL驱动程序是42.6.0,是最新稳定版本。(我尝试过一些旧版本,包括9.x版本;但都不行。)我们使用的是PostgreSQL 13.10。
这尤其令人困惑,因为上周一切都很正常,然后我在星期一打开电脑时一切都坏了。我在我的Mac上有2023.2版本,但这个问题在那之前就出现了。我甚至完全卸载了IDEA并删除了~/Library
中的所有JetBrains/IDEA设置,重新开始,但没有任何改变。希望在这里找到一些见解,因为我和我的团队已经没有别的想法了,除了驱魔术。谢谢。
附加信息来自评论:
/etc/hosts
,去掉注释后,是这样的:
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
在日志中没有太多有用的线索。我只看到UI中的“连接尝试失败”消息。此消息与DatabaseConnectionEstablisher
关联,ID为08001。
sudo lsof -i :5432
的结果如下:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
com.docke 535 [me] 223u IPv6 0xdc13b7dc139e8629 0t0 TCP *:postgresql (LISTEN)
英文:
This seems like it should be a very basic problem to fix, but it's driven me to the brink of madness and cost me days of work.
I'm trying to run Java integration tests locally but they're failing due to a PSQLException in connecting. In my terminal, I'm pinging localhost
fine and I can successfully telnet to 5432. I can get to the local DB using Postico as well. To try to get at this problem, I'm trying to configure a database within IDEA. It's pretty elementary. The URL is jdbc:postgresql://localhost:5432/postgres
and all the other settings match my successful Postico configuration. I haven't changed anything in the Drivers tab; the PostgreSQL driver is 42.6.0, the latest stable. (I tried a few older ones, including all the way back to 9.x; no dice.) We use PostgreSQL 13.10.
This is especially puzzling because everything was fine last week, then I opened up my computer on Monday and everything was broken. I have 2023.2 on my Mac, but this issue was happening before that. I even completely uninstalled IDEA and deleted all of the JetBrains/IDEA settings from ~/Library
to start again, but no change. Hoping to find some insights here, as my team and I are completely out of ideas short of an exorcism. Thanks.
Additional info added from comments:
/etc/hosts
, excluding comments, is:
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
There isn't much clueful in the logging. I see only "The connection attempt failed" in the UI. That message is associated with DatabaseConnectionEstablisher
, ID 08001.
sudo lsof -i :5432
yields:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
com.docke 535 [me] 223u IPv6 0xdc13b7dc139e8629 0t0 TCP *:postgresql (LISTEN)
答案1
得分: 2
根据 lsof
输出,数据库服务器正在监听 IPv6 接口而不是 IPv4。
要强制 IntelliJ IDEA/DataGrip 使用 IPv6,请在 Help | Edit Custom VM Options 中添加以下内容,然后重新启动 IDE:
-Djava.net.preferIPv4Stack=false
-Djava.net.preferIPv6Addresses=true
您可能还需要在数据源的高级设置中添加相同的内容,即 VM options 字段,以及在您的测试/Java应用程序的 VM 选项中添加。
英文:
According to lsof
output the database server is listening on IPv6 interface instead of IPv4.
To force IntelliJ IDEA/DataGrip use IPv6 you can add the following in Help | Edit Custom VM Options and restart the IDE:
-Djava.net.preferIPv4Stack=false
-Djava.net.preferIPv6Addresses=true
You may also need to add the same in the data source Advanced settings, VM options field and in the VM Options for your tests / Java apps.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论