Herokuというクラウドサービスを利用して、データベースを使えるようにしていきます。
簡単に使えるようになりますし、個人で使うレベルなら無料で使えますので、データベースを触ってみたい!!という方は、ぜひ参考にしてもらえればと思います!
Node.jsからこちらのデータベースを使っていく想定です。
もしサーバサイドの開発もやってみたい方がいれば、こちらの記事も参考にしてみてください!

HEROKUでデータベースを使えるようにするのは、本当に簡単なのでぜひ参考にしてもらえると嬉しいです!
- データベースを触ってみたい!
- 無料のデータベースがいい!
- 簡単にデータベースを構築したい!
- Herokuってなんですか?
-
コンテナベースのクラウド型PaaSです。
アプリケーションのデプロイ環境やデータベースを無料で使う事ができます。(制限あり)
メールアドレスさえあれば、登録できるのでお手軽に使用することができます! - PaaSってなんですか?
-
Platform as a Serviceと言って、アプリケーションソフトが稼働するためのハードウェアやOSなどのプラットフォーム一式を、インターネット上のサービスとして提供する事です。
Google App EngineやMicrosoft Azureなんかもこのサービスの一つになります!
HEROKUに会員登録しよう!

最近は、会員登録すら大変なんじゃよ。。。
まずは、会員登録をしてください。
メールアドレスがあれば、登録できるのでお手軽に登録できると思います。
クレジットカードも登録は不要です!
まずは、Herokuの公式ページにいきましょう!


このようなページが表示されていると思います!
右上の「新規登録」から登録していきます!


登録するための情報を、入力していきましょう!
会社名は必須ではないので、入力は自由です!
![0 コ 0 、 0 に 属
H E R 0 K U
す で に ア カ ウ ン ト を お 持 ち で す か ?
ロ グ イ ン
無 料 で 新 規 登 録 し 、 す ぐ
Heroku を 体 験 で き ま す
@ 無 料 の ア カ ウ ン ト
に 字
ア プ リ の 作 成 、 デ ー タ ベ ー ス の 接 続 と ア ト オ ン サ ー ヒ
名 前 '
ス の 利 用 、 チ - ム て の # 向 作 業 を 、 第 料 て を
名 前
メ - ル ア ト レ ス '
メ - ル ア ト レ ス
◎ お 客 様 の ア プ リ プ ラ ッ ト フ
ア プ リ の を め の 、 洋 や 阜 い ス ケ ー リ ン ク を で き
る 、 発 そ し て 本 番 の た の の プ ラ ッ ト フ ォ - ム を
G) い ま す ぐ デ ブ ロ イ
日 本 リ 叩 ]
コ ー ト か ら ア プ リ の 物 働 ま て を 分 当 位 て 。 デ ブ ロ イ 、
ス ケ - ル し て お 客 橋 の ア プ リ を 世 界 に 配 信 。
主 な 簧 発 ま ・
言 語 を 選 択 し て ( た さ い
0
「 一 -1 私 は ロ ホ ッ ト て は あ り
ま せ ん
プ ラ イ 丿 ー シ ー ・ 第 一 川 め](https://teech-lab.com/wp-content/uploads/2021/03/image-25-1024x517.png)
![0 コ 0 、 0 に 属
H E R 0 K U
す で に ア カ ウ ン ト を お 持 ち で す か ?
ロ グ イ ン
無 料 で 新 規 登 録 し 、 す ぐ
Heroku を 体 験 で き ま す
@ 無 料 の ア カ ウ ン ト
に 字
ア プ リ の 作 成 、 デ ー タ ベ ー ス の 接 続 と ア ト オ ン サ ー ヒ
名 前 '
ス の 利 用 、 チ - ム て の # 向 作 業 を 、 第 料 て を
名 前
メ - ル ア ト レ ス '
メ - ル ア ト レ ス
◎ お 客 様 の ア プ リ プ ラ ッ ト フ
ア プ リ の を め の 、 洋 や 阜 い ス ケ ー リ ン ク を で き
る 、 発 そ し て 本 番 の た の の プ ラ ッ ト フ ォ - ム を
G) い ま す ぐ デ ブ ロ イ
日 本 リ 叩 ]
コ ー ト か ら ア プ リ の 物 働 ま て を 分 当 位 て 。 デ ブ ロ イ 、
ス ケ - ル し て お 客 橋 の ア プ リ を 世 界 に 配 信 。
主 な 簧 発 ま ・
言 語 を 選 択 し て ( た さ い
0
「 一 -1 私 は ロ ホ ッ ト て は あ り
ま せ ん
プ ラ イ 丿 ー シ ー ・ 第 一 川 め](https://teech-lab.com/wp-content/uploads/2021/03/image-25-1024x517.png)
登録情報を入力すると、登録したメールアドレスにメールが届くと思うので、そこにあるURLをクリックしてパスワード設定画面を開きます!
忘れないかつ予想されにくいパスワードを登録しましょう!


英語がわからない方は、Google翻訳やDeep-Lなどで翻訳して読みましょう!
問題なければ、「Accept」をクリックします!


こんな画面になっていれば、登録完了です!!


クレジットカードの登録がいらないのは、嬉しいですね(笑)
勝手に課金が発生していた!!なんてこともないですし!
データベースを構築しよう!
データベースを構築と言っても、ボタンをポチポチするだけです!
難しくないので、安心してください!
新しいappを作成しよう!
Herokuはappという単位で構築していきます。
app = サーバみたいな認識でいるといいかと思います!
画面中央にある「Create new app」をクリックしてください!


下記のような画面が表示されたと思います!
「App name」は、お好きな名前を登録してもらって問題ありません。
「Chose a region」も、お好きな場所でいいです。今回は、United Statesにしておきます!
入力したら「Create app」をクリックしましょう!


下のような画面になれば、appの作成は完了です!!


Heroku Postgresを追加しよう!
Heroku Postgresは、Herokuが直接提供するマネージドSQLデータベースサービスです!
PostgreSQLドライバーを利用しています!
- PostgreSQLってなんですか?
-
オープンソースのRDBMS(リレーショナルデータベース管理システム)です。
表形式(列と行をもつテーブル)でデータを管理する事ができます。PostgreSQL以外だと、MySQLやQracle DatabaseなどもRDBMSになります。
Herokuでは、appにHeroku Postgresを追加することでPostgreSQLが使えるようになります!
Add-onsで、「Heroku Postgres」を検索して選択してください。
検索は、Quickiy add add-ons from Elementsから検索できます。


今回は、無料で利用したいのでPlan nameは、「Hobby Dev – Free」を選択します!


追加できたら、Add-onsに「Heroku Postgres」が表示されていることを確認してください!


これでデータベースが使えるようになりました!!簡単ですよね!
続いては、このデータベースにA5M2で接続してみようと思います!
A5M2でデータベースに接続しよう!
データベースが使えるようになったので、実際にデータベースに接続していこうと思います!
今回は、データベースに接続するツールとしてA5M2を利用していきます!
- A5M2ってなんですか?
-
A5:SQL Mk-2は、SQLクライアントツールです。
フリーソフトながらに、高性能で使いやすいツールになっています。SQLでデータベースを操作できるのはもちろん、GUIでデータベースを操作することも可能です。
また、ER図やDB設計書などを自動生成することもできます。
では、早速接続していきましょう!!
Herokuで接続情報を確認しよう!
まずは、Herokuで接続するための情報を確認します。
先ほど登録したAdd-onsに表示されている「Heroku Postgres」をクリックしてください!


下の画面に遷移したと思います。
メニューバーから「Settings」に移動してください!
ちなみに、この画面に表示されている内容は、無料で使える条件です。
コネクションが20 ,データが10,000レコードは無料で使えます!


Settingsに表示されている「Host」「Database」「User」「Port」「Password」が今回必要な接続情報です!


データベースに接続しよう!
続いて、接続情報を用いて実際にデータベースに接続していきます。
A5M2をインストールしていない方は、こちらからインストールしてください!
Zipがダウンロードできるので、解凍して任意の場所(Cドライブ直下やProgram Files配下でも大丈夫です。)に保存してください。
回答したフォルダ内の、「.exe」というファイルを起動したら使えます!
A5M2を起動したらこんな画面になっていると思います。
メニューバー>データベース>データベースの追加と削除を選択してください!
![A5:SQL Mk-2 64bit edition 朝 on 2.1 0 beta 23 - [ Que リ -1 ]
「 フ ァ イ F ) 編 第 ( E ) デ - タ ベ - ス ( D ) 50. ( 5 ) ツ -MT) 表 示 ( Ⅵ ウ イ ン ド ウ ( W ) 設 定 ( 円 ヘ ル プ ( H )
ー デ ー タ ベ ・ - ス を 選 択 し て 下 さ い )
「 - ト 。 ト 凝 物 朝 ロ 回 物 ・](https://teech-lab.com/wp-content/uploads/2021/03/image-38-1024x555.png)
![A5:SQL Mk-2 64bit edition 朝 on 2.1 0 beta 23 - [ Que リ -1 ]
「 フ ァ イ F ) 編 第 ( E ) デ - タ ベ - ス ( D ) 50. ( 5 ) ツ -MT) 表 示 ( Ⅵ ウ イ ン ド ウ ( W ) 設 定 ( 円 ヘ ル プ ( H )
ー デ ー タ ベ ・ - ス を 選 択 し て 下 さ い )
「 - ト 。 ト 凝 物 朝 ロ 回 物 ・](https://teech-lab.com/wp-content/uploads/2021/03/image-38-1024x555.png)
データベースの追加と削除のダイアログが表示されたと思いますので、左下の「追加」ボタンを選択しましょう!


追加するデータベースの接続タイプを選択する画面になるので、専用接続 オープンソースデータベース(クライアント/サーバ)>「PostgreSQL(直接接続)」を選択してください。


データベースの内容を登録と言う画面が表示されたと思います。
ここで、先ほど確認したHeroku Postgresの接続情報を入力します。
A5M2 | Heroku |
---|---|
サーバ名 | Host |
ポート番号 | Port(5432) |
データベース名 | Database |
ユーザID | User |
パスワード | Password |


接続情報の入力したら、SSLタブに移動します。
SSLモードを、「優先(SSL接続を優先)」に変更して、
「サーバ証明書を信用する」にチェックを入れます。


ここまでできたらテスト接続をしてみましょう!
「接続に成功しました。」が表示されたらOKです。
設定を登録して、接続完了です!
接続名を最後に登録するのですが、わかりやすい名前にしておけば大丈夫です。(今回は、HEROKUにしました。)


テーブルを作ってみよう!
接続できたことは、確認したので実際にテーブルを作ってみましょう!
左上が先ほど登録した接続情報名になっていることを確認してください。
テーブルを作るので、「CTEATE TABLE」のSQLを実行します。
CREATE TABLE memo(
id serial
, title TEXT
, text TEXT
,PRIMARY KEY (id)
);


上記までできたら、Ctrl+Enterを押して実行します。(「;」の内側で実行する必要があるので注意してください)
すると、public内のテーブルに「memo」と言うテーブルが追加されたと思います。(表示されていない場合は、データベースで右クリック>全てのデータベース情報の再読み込みをしてください。)


memoをダブルクリックすると、テーブルの情報を見る事ができます!
まだデータはないですが、カラム欄などはみれますよ!


最後に、Herokuのダッシュボードを確認しておきます。
TABLE数が1になってますね!!


まとめ
これでデータベースを使うことができるようになりました。
無料で使えるので、データベースの使い方などの勉強などに活用してみてください!
今回構築したデータベースは、NodeでWeb APIを開発する際に使っていこうと思います。
バックエンド開発に興味がある方は、こちらの記事も読んでみてはいかがでしょうか!


最終的には、Node.jsをデプロイする手順も記事にしようと思います!