2
1

More than 5 years have passed since last update.

Elastic BeanStalkで作成したPostgreSQLにpsqlコマンドで接続する

Last updated at Posted at 2016-11-20

目的

Elastic BeanStalkで作成したPostgreSQLにシェルで接続する

前提

Elastic BeanStalkでアプリが動いている。(EC2, RDSデータベースが起動し、連携できている。)

方法

方法は2種類。セキュリティグループを変更するのは面倒だったので、今回は後者(2)の方法をとった。
1. RDSのセキュリティーグループを変更して直接psqlコマンド
2. EC2にSSHログインして、EC2のシェルからpsqlコマンド

手順

  1. EC2にログインする
  2. psqlコマンドで接続する

EC2にログインする

.pemファイルのダウンロード

  1. AWSコンソール上で、EC2→キーペア→キーペアの作成→キーペア名入力後作成ボタンを押す

    https://gyazo.com/1ef2b2b3b9c8fec9697ce7b282faf328

  2. ボタンを押した後に自動的にダウンロードされた xxx.pem ファイルを /Users/自分のユーザー名/.ssh/ にコピーする

    https://gyazo.com/9acb474fc2986655284daf17aac31cdc

  3. xxx.pemファイルの権限(パーミッション)を適切に設定する
    キーの権限が適切でないとsshするときにエラーが出るのでchmodコマンドを使って権限を適切にする
    適切な権限はchmod 400 (AWSヘルプ

    ここで自分のユーザー名はjunaraとする
    
    現在の権限を確認
    junara@macbook $ ls -la /Users/junara/.ssh/test.pem
    -rw-r--r--@ 1 junara  staff  1696 11 20 00:25 /Users/junara/.ssh/test.pem
    
    権限を変更
    junara@macbook $ chmod 0400 /Users/junara/.ssh/test.pem
    
    権限の変更を確認
    junara@macbook $ ls -la /Users/junara/.ssh/test.pem
    -r--------@ 1 junara  staff  1696 11 20 00:25 /Users/junara/.ssh/test.pem
    
    

.pemファイルをEC2に登録

  1. AWSコンソール上でElastic Beanstalk→自分のアプリケーション→設定→インスタンス

    https://gyazo.com/523be358fd650dd18db8e12757c421a3

  2. EC2キーペアの箇所(矢印箇所)に先に設定したキーペアを入力して、ページ右下の適用をクリックする。(反映までしばらく時間かかる)

    https://gyazo.com/e3a5ac0820c00f33bb5fe47a9cadac67

configファイル作成

ssh -i 鍵ファイル ec2ユーザー名@インスタンスのパブリックDNS名 を入力しても良いがconfigファイルを作ると、sshに接続するときのコマンドが短くすむので楽ちん。
よって、今回はconfigファイルを作る方法を説明する。

  1. configファイルを作成するための情報用意
    configファイルを作成するために必要な情報は以下の通り

    • Host:sshコマンドを入力するときの任意のエイリアス。今回はec2-appとした。
    • HostName:EC2のユーザー名とインスタンスのパブリックDNS名: AWSコンソール上で「EC2→インスタンス→インスタンスにチェック→接続 ボタンを押す」で下記の画像で示す赤枠に表示される文字列
    • Port:22
    • user:下記画像参考。特に設定していなければec2-userだと思う。
    • IdentityFile:鍵ファイルのパス: .pemファイルのダウンロード で作成した通り。 /Users/junara/.ssh/test.pem

    https://gyazo.com/2631608eeabc382ab556a41fb689ff4d

  2. configファイル作成

    • /Users/junara/.ssh 下にconfigというファイル名でファイルを作成する。
    • 入力例は下記の通り
    /Users/junara/.ssh/config
    Host ec2-app
      HostName  ec2-XX-XXX-XX-XXX.ap-northeast-1.compute.amazonaws.com
      Port      22
      user      ec2-user
      IdentityFile  /Users/junara/.ssh/test.pem
    

EC2へ接続

sshでEC2へ接続する。既にconfigファイルで設定しているので、下記を入力するだけ。

ssh Host名

以下接続例。

junara@macbook $ ssh ec2-app
The authenticity of host 'ec2-XX-XXX-XX-XXX.ap-northeast-1.compute.amazonaws.com (XX-XXX-XX-XXX)' can't be established.
ECDSA key fingerprint is SHA256:---------------------------------------.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'ec2-XX-XXX-XX-XXX.ap-northeast-1.compute.amazonaws.com,XX-XXX-XX-XXX' (ECDSA) to the list of known hosts.
 _____ _           _   _      ____                       _        _ _
| ____| | __ _ ___| |_(_) ___| __ )  ___  __ _ _ __  ___| |_ __ _| | | __
|  _| | |/ _` / __| __| |/ __|  _ \ / _ \/ _` | '_ \/ __| __/ _` | | |/ /
| |___| | (_| \__ \ |_| | (__| |_) |  __/ (_| | | | \__ \ || (_| | |   <
|_____|_|\__,_|___/\__|_|\___|____/ \___|\__,_|_| |_|___/\__\__,_|_|_|\_\
                                       Amazon Linux AMI

This EC2 instance is managed by AWS Elastic Beanstalk. Changes made via SSH 
WILL BE LOST if the instance is replaced by auto-scaling. For more information 
on customizing your Elastic Beanstalk environment, see our documentation here: 
http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html
[ec2-user@ip-xxx-xx-x-xxx ~]$ 

PostgreSQLに接続する

  1. PostgreSQLの詳細情報表示
    AWSコンソール上でElastic Beanstalk→自分のアプリケーション→設定→RDSの右上の箇所を押す

    https://gyazo.com/9c4ea9baad91373a65c2fa185f0ed992

  2. PostgreSQLの詳細情報取得
    接続には、host、port、username、password、dbnameが必要。下記画像を参考に情報をメモする。なお、特別な設定をしなければportは5432、dbnameはebdbだと思う。

    https://gyazo.com/3e0261ae744c169ac6eab093cea827a9

  3. PostgreSQLへ接続する
    ec2のシェルからpsqlコマンドで接続する

$ psql --host=xxxxxxxxx.xxxxxxxxxxx.ap-northeast-1.rds.amazonaws.com --port=5432 --username=yyyyy --password --dbname=zzzz 

ユーザ yyyyy のパスワード: 
psql (9.4.9, サーバー 9.5.4)
注意: psql メジャーバージョン 9.4, サーバーバージョン 9.5.
         psql の機能の中で、動作しないものがあるかもしれません。
SSL接続(プロトコル: TLSv1.2, 暗号化方式: ECDHE-RSA-AES256-GCM-SHA384, ビット長: 256, 圧縮: オフ)
"help" でヘルプを表示します.

zzzz=> 

完成!

トラブルシューティング用

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