0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

JDBCからPostgreSQLに接続できない

Posted at

エラー

「localhost:5432 への接続が拒絶されました。ホスト名とポート番号が正しいことと、postmaster がTCP/IP接続を受け付けていることを確認してください。」

環境

  • IntelliJ IDEA community版 (2020.2.4)
  • WSL2(Ubuntu 20.04)
  • PostgreSQL 12.7
  • PostgreSQLでテーブル作成が完了していること(テーブルの中身は任意)

原因

  • PostgreSQLが起動していない
  • 接続するURL(ホスト名、ポート番号)が誤っている

対処法

PostgreSQLが起動しているかWSL2上で確認しましょう。

こうなっていたらPostgreSQLは起動していません。

# /etc/init.d/postgresql status
12/main (port 5432): down

rootユーザでPostgreSQLを起動させましょう。
5432番ポートで起動したことが確認できます。

# /etc/init.d/postgresql start
 * Starting PostgreSQL 12 database server                     [ OK ]

# /etc/init.d/postgresql status
12/main (port 5432): online

IntelliJ IDEAでJavaプログラムを再実行しましょう。

参考:エラーログ

"C:\Program Files\Java\jdk-11\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.2.4\lib\idea_rt.jar=63885:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.2.4\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\11017\AppData\Local\JetBrains\IdeaIC2020.2\compile-server\demo_f82fe5f3\scratches\out;C:\Users\11017\IdeaProjects\SobcJavaBasicGradle\build\classes\java\main;C:\Users\11017\IdeaProjects\SobcJavaBasicGradle\build\resources\main;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-web\2.4.1\fb70dcc55475177dd404c5577c6d64b6c591c9cd\spring-boot-starter-web-2.4.1.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-json\2.4.1\5a4b7088c88be531cb416ef218da3e19309f8cea\spring-boot-starter-json-2.4.1.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter\2.4.1\15d81ffe81e26de37986d491e1cf4c1e3a324ca3\spring-boot-starter-2.4.1.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-tomcat\2.4.1\92c0d7938bdf47fb9d3781aef94b07f5a940a263\spring-boot-starter-tomcat-2.4.1.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.springframework\spring-webmvc\5.3.2\6c1106b59db4c91682b740d2deaa3f02400ea8c8\spring-webmvc-5.3.2.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.springframework\spring-web\5.3.2\5ea6e1536ee6a70bb5c2efbd579c688c09a93a4b\spring-web-5.3.2.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.datatype\jackson-datatype-jdk8\2.11.3\dcdfce3e4af9da18d69b1be6f3d7c91256105831\jackson-datatype-jdk8-2.11.3.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.datatype\jackson-datatype-jsr310\2.11.3\dd53d93fd448b345eeb75bec336d9c91dc56b796\jackson-datatype-jsr310-2.11.3.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.module\jackson-module-parameter-names\2.11.3\ab66d5c5b8008161ae89be8a3d98efff9ef1b915\jackson-module-parameter-names-2.11.3.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-databind\2.11.3\4f7b27416934dc929bb6c2d2c5fe521829e6a4ec\jackson-databind-2.11.3.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-autoconfigure\2.4.1\9af62d8d15ee71efb9baf35dc5263fc953230ab1\spring-boot-autoconfigure-2.4.1.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot\2.4.1\92bf6245c590b56ecb10d8d39ffe04ab0cdfee9\spring-boot-2.4.1.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-logging\2.4.1\5e83cd807e8fe6a25dd36a37ad9222e9d1309336\spring-boot-starter-logging-2.4.1.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\jakarta.annotation\jakarta.annotation-api\1.3.5\59eb84ee0d616332ff44aba065f3888cf002cd2d\jakarta.annotation-api-1.3.5.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.springframework\spring-core\5.3.2\152489ed8223a6ad19065a3cd1ee6b9e20c0b82f\spring-core-5.3.2.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.yaml\snakeyaml\1.27\359d62567480b07a679dc643f82fc926b100eed5\snakeyaml-1.27.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.apache.tomcat.embed\tomcat-embed-websocket\9.0.41\b6e5fdc2a4088c340d0916468d7e2ecff71d83d\tomcat-embed-websocket-9.0.41.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.apache.tomcat.embed\tomcat-embed-core\9.0.41\a43e9711e85073187d04b137882b4b7957180ef0\tomcat-embed-core-9.0.41.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.glassfish\jakarta.el\3.0.3\dab46ee1ee23f7197c13d7c40fce14817c9017df\jakarta.el-3.0.3.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.springframework\spring-context\5.3.2\83ff3909a582eceb5c8ae152f1e510672106f42b\spring-context-5.3.2.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.springframework\spring-aop\5.3.2\c01aecb495a37786b5d6b7e9701c6f78353b61e6\spring-aop-5.3.2.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.springframework\spring-beans\5.3.2\289d8047f7cc524d60ca0c4cc6fedb8cb003e02d\spring-beans-5.3.2.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.springframework\spring-expression\5.3.2\31cfebc4f74de54ddf4298cc6fa4de4090486c41\spring-expression-5.3.2.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-core\2.11.3\c2351800432bdbdd8284c3f5a7f0782a352aa84a\jackson-core-2.11.3.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-annotations\2.11.3\25d4e9c777e7a8805c4a000a8629d3009c779c9b\jackson-annotations-2.11.3.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\ch.qos.logback\logback-classic\1.2.3\7c4f3c474fb2c041d8028740440937705ebb473a\logback-classic-1.2.3.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.apache.logging.log4j\log4j-to-slf4j\2.13.3\966f6fd1af4959d6b12bfa880121d4a2b164f857\log4j-to-slf4j-2.13.3.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.slf4j\jul-to-slf4j\1.7.30\d58bebff8cbf70ff52b59208586095f467656c30\jul-to-slf4j-1.7.30.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.springframework\spring-jcl\5.3.2\5a9fc63ea825fe0de40cefd6d4985490ceabefbb\spring-jcl-5.3.2.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\ch.qos.logback\logback-core\1.2.3\864344400c3d4d92dfeb0a305dc87d953677c03c\logback-core-1.2.3.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.slf4j\slf4j-api\1.7.30\b5a4b6d16ab13e34a88fae84c35cd5d68cac922c\slf4j-api-1.7.30.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.apache.logging.log4j\log4j-api\2.13.3\ec1508160b93d274b1add34419b897bae84c6ca9\log4j-api-2.13.3.jar;C:\Users\11017\IdeaProjects\SobcJavaBasicGradle\build\classes\java\test;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-test\2.4.1\50147ab5f3b431a397d27e2c0af313b9147cd058\spring-boot-starter-test-2.4.1.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-test-autoconfigure\2.4.1\1890c6d00ad76a01d3a0011634b0d77e42142ccb\spring-boot-test-autoconfigure-2.4.1.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-test\2.4.1\f8fc754a9e85dbebc9d8c58f8aa3f5b55c831de9\spring-boot-test-2.4.1.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\com.jayway.jsonpath\json-path\2.4.0\765a4401ceb2dc8d40553c2075eb80a8fa35c2ae\json-path-2.4.0.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\jakarta.xml.bind\jakarta.xml.bind-api\2.3.3\48e3b9cfc10752fba3521d6511f4165bea951801\jakarta.xml.bind-api-2.3.3.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.assertj\assertj-core\3.18.1\aaa02680dd92a568a4278bb40aa4a6305f632ec0\assertj-core-3.18.1.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.hamcrest\hamcrest\2.2\1820c0968dba3a11a1b30669bb1f01978a91dedc\hamcrest-2.2.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.junit.jupiter\junit-jupiter\5.7.0\3152d152da916ccbb0715f89f7f873f45362ad7f\junit-jupiter-5.7.0.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.mockito\mockito-junit-jupiter\3.6.28\23149890c3b6047604a682aa3d47151d440e1bfa\mockito-junit-jupiter-3.6.28.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.mockito\mockito-core\3.6.28\ad16f503916da658bd7b805816ae3b296f3eea4c\mockito-core-3.6.28.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.skyscreamer\jsonassert\1.5.0\6c9d5fe2f59da598d9aefc1cfc6528ff3cf32df3\jsonassert-1.5.0.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.springframework\spring-test\5.3.2\34664d994a70997529e3dc86c56ccf4deef61490\spring-test-5.3.2.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.xmlunit\xmlunit-core\2.7.0\4d014eac96329c70175116b185749765cee0aad5\xmlunit-core-2.7.0.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\net.minidev\json-smart\2.3\7396407491352ce4fa30de92efb158adb76b5b\json-smart-2.3.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\jakarta.activation\jakarta.activation-api\1.2.2\99f53adba383cb1bf7c3862844488574b559621f\jakarta.activation-api-1.2.2.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.junit.jupiter\junit-jupiter-params\5.7.0\521dbecace93d5d7ef13a74aab231befd7954424\junit-jupiter-params-5.7.0.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.junit.jupiter\junit-jupiter-api\5.7.0\b25f3815c4c1860a73041e733a14a0379d00c4d5\junit-jupiter-api-5.7.0.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\net.bytebuddy\byte-buddy\1.10.18\20240291b4f14ffe986e45468b1f1a3c15edc37c\byte-buddy-1.10.18.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\net.bytebuddy\byte-buddy-agent\1.10.18\1070e69ef571b326d91819b57bd06fd7efc60819\byte-buddy-agent-1.10.18.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.objenesis\objenesis\3.1\48f12deaae83a8dfc3775d830c9fd60ea59bbbca\objenesis-3.1.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\com.vaadin.external.google\android-json\0.0.20131108.vaadin1\fa26d351fe62a6a17f5cda1287c1c6110dec413f\android-json-0.0.20131108.vaadin1.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\net.minidev\accessors-smart\1.2\c592b500269bfde36096641b01238a8350f8aa31\accessors-smart-1.2.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.apiguardian\apiguardian-api\1.1.0\fc9dff4bb36d627bdc553de77e1f17efd790876c\apiguardian-api-1.1.0.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.junit.platform\junit-platform-commons\1.7.0\84e309fbf21d857aac079a3c1fffd84284e1114d\junit-platform-commons-1.7.0.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.opentest4j\opentest4j\1.2.0\28c11eb91f9b6d8e200631d46e20a7f407f2a046\opentest4j-1.2.0.jar;C:\Users\11017\.gradle\caches\modules-2\files-2.1\org.ow2.asm\asm\5.0.4\da08b8cce7bbf903602a25a3a163ae252435795\asm-5.0.4.jar;C:\Users\11017\Downloads\postgresql-42.2.14.jar scratch_7
org.postgresql.util.PSQLException: localhost:5432 への接続が拒絶されました。ホスト名とポート番号が正しいことと、postmaster がTCP/IP接続を受け付けていることを確認してください。
	at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:285)
	at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
	at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:217)
	at org.postgresql.Driver.makeConnection(Driver.java:458)
	at org.postgresql.Driver.connect(Driver.java:260)
	at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
	at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
	at scratch_7.main(scratch_7.java:29)
Caused by: java.net.ConnectException: Connection refused: connect
	at java.base/java.net.PlainSocketImpl.waitForConnect(Native Method)
	at java.base/java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:107)
	at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)
	at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)
	at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)
	at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403)
	at java.base/java.net.Socket.connect(Socket.java:591)
	at org.postgresql.core.PGStream.<init>(PGStream.java:81)
	at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:93)
	at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:197)
	... 7 more

プロセスは終了コード 0 で完了しました

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?