目次
はじめに
前回までで、HerokuへSpring BootやAngularで作成したアプリをデプロイする手順について解説してきました。
これら以外にも、HerokuのPaaS環境ではDBMSを利用することも可能です。Herokuは標準のDBMSとしてPostgresをサポートしています。
ということで、
今回は、HerokuにデプロイしたSpring Bootアプリから、Heroku Postgresを利用する手順について解説します。
ちなみに、Heroku Postgresは10,000レコード、データ容量7.2MB以内であれば、
無料で利用できます!!
環境
- OS:OS X 10.11 El Capitan
- STS:3.8.4.RELEASE
- Git:2.10.1(Apple Git-78)
前提
以下が完了している前提で説明します。

Heroku Postgresの利用手順
HerokuへデプロイしたSpring BootからHeroku Postgresを利用するまでの手順は以下です。
- Spring Bootアプリ作成
- Heroku上でアプリ作成
- Heroku Postgres利用設定
- Herokuデプロイ用設定
- Spring Bootアプリのプッシュ&デプロイ
1.Spring Bootアプリ作成
作成するアプリはなんでもよいのですが、
事前にローカルで動くことを確認しておいてください。
本記事では、以下で作成したアプリを利用します。

2.Heroku上でアプリ作成
新規アプリを作成します。
アプリ名を入力します。
以上でHeroku上でのアプリ作成は完了です。
3.Heroku Postgres利用設定
pom.xmlの依存関係にpostgresqlを追加します。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>rest-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>rest-api</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.lazyluke</groupId>
<artifactId>log4jdbc-remix</artifactId>
<version>0.2.7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
spring.datasource.url=${JDBC_DATABASE_URL}
spring.datasource.username=${JDBC_DATABASE_USERNAME}
spring.datasource.password=${JDBC_DATABASE_PASSWORD}
spring.jpa.hibernate.ddl-auto=update
logging.level.jdbc=OFF
logging.level.jdbc.sqltiming=DEBUG
4.Herokuデプロイ用設定
1で作成したプロジェクト内にProcfileとsystem.propertiesを追加します。
web: java $JAVA_OPTS -jar target/*.jar --server.port=$PORT
java.runtime.version=1.8
以上でHerokuデプロイ用の設定は完了です。
5.Spring Bootアプリのプッシュ&デプロイ
ターミナルを開き、カレントディレクトリをデプロイ対象のプロジェクト内に移します。
$ cd rest-api
Herokuへログインします。
$ heroku login
$ git init
$ heroku git:remote -a spring-boot-rest-api
$ git add .
$ git commit -am "make it better"
$ git push heroku master
まず、curlコマンドでRestAPIをコールして商品「もも」をDBへ登録します。
$ curl https://spring-boot-rest-api.herokuapp.com/api/items -i -XPOST -H "Content-Type: application/json" -d "{\"name\":\"もも\",\"price\":\"1000\",\"imgPath\":\"peach.jpg\"}"
HTTP/1.1 201
Server: Cowboy
Connection: keep-alive
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Tue, 11 Jul 2017 12:53:57 GMT
Via: 1.1 vegur
{"id":1,"name":"もも","price":1000,"imgPath":"peach.jpg"}
https://spring-boot-rest-api.herokuapp.com/api/items
上の画面が表示できたら、
DBから登録した商品「もも」が抽出できているのでOKです。
データベース接続情報確認
データベース接続ツールなどでHeroku Postgresに接続したい場合があるので、
Heroku Postgresのデータベース接続情報を確認しておきます。
まず、HerokuダッシュボートからHeroku Postgresをクリックします。
下の画面では、データベース上に作成しているテーブルとレコードの数が確認でき、
「View Credentials…」をクリックするとDB接続情報が確認できます。
※DB接続ツール(A5M2やPSequel)からHeroku PostgresへはSSL接続のみ可能です!!
おわりに
今回はHerokuにデプロイしたSpring BootアプリからHeroku Postgresを利用する手順について解説しました。
「ちょっとした開発でわざわざDBサーバ用意したくない。。。」
「とにかく無料でWebアプリをインターネット公開したい!!」
という方はぜひ利用してみてください。
こんにちは。初歩的な質問で申し訳ないのですがProcfileの$JAVA_OPTS,$PORTには何を入力したらよいのでしょうか..?