1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Spring Boot + PostgreSQL(Docker) をローカルで繋ぐ手順(初心者がつまずいた点つき)

1
Posted at

はじめに

Spring Boot のアプリを、Docker で動かした PostgreSQL に繋いで起動しました。
同じところを触り始めた初心者向けに、手順とつまずいた点をまとめます。

環境

  • Spring Boot 3.5.14
  • Java 21
  • Gradle
  • PostgreSQL 16.13(Docker)
  • macOS

手順①:DB(PostgreSQL)を起動する

PostgreSQL を docker compose up -d で起動します。
アプリは起動時に DB に繋ぐので、先に DB を起動しておきます。

手順②:接続設定(application.properties)

application.properties に接続情報を書きます。
URL・ユーザー名・パスワードの3つは、自分の DB に合わせます。

spring.datasource.url=jdbc:postgresql://localhost:5432/データベース名
spring.datasource.username=ユーザー名
spring.datasource.password=パスワード

手順③:アプリを起動する

設定ができたら、./gradlew bootRun でアプリを起動します。

起動成功の見分け方

ログに次の3つが出れば、起動成功です。

HikariPool-1 - Start completed     ← DB 接続OK
Tomcat started on port 8080        ← Web 起動
Started BackendApplication ...     ← 起動完了

つまずき:接続拒否 と 404 の違い

ブラウザで開くと2つの状態がありました。

  • 接続拒否(CONNECTION_REFUSED) → アプリが起動していない
  • Whitelabel / 404 → アプリは起動している(中身がまだ無いだけ)

最初は 404 を見て「失敗した」と思いましたが、実はアプリが起動できている成功のサインでした。

1
3
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
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?