40
40

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 5 years have passed since last update.

【AWS】cliでEC2からRDSに接続する【postgresql】

Last updated at Posted at 2016-03-07

cliが苦手でRDS初心者なエンジニアです。つまづいたので、備忘録としてまとめ。

前提:

  • EC2ひとつ、RDSひとつが用意されている
  • EC2にSSH接続している

特に何も考えずに、

$ psql

をやると次のようなエラーが出るはず・・・

psql: could not connect to server: No such file or directory
      Is the server running locally and accepting
      connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

手順はふたつ

  • EC2にRDS用SecurityGroupの設定
  • 間違えずにpsqlコマンドをたたく

EC2にRDS用SecurityGroupの設定

  1. EC2のコンソールを開く
  2. EC2に紐付いているSecurityGroupを探して選択
    • InstanceのDescriptionタブから飛んでも良い
    • 直接左ペインのSecurityGroupsから辿っても良い
  3. 所望のSecurityGroupにたどり着いたら、InboundタブにてEditボタン選択
  4. 下のように設定できればOK
    postgre_setting_sg.PNG

*Sourceの設定が0.0.0.0/0になっていて、誰でもアクセスできるようになっているので、適宜アクセスする人を絞ってください

間違えずにpsqlコマンドをたたく

  1. 必要な情報をRDSのコンソールで確認する(ここで必要なのはRDSのEndpointとUsername)
    RDS_setting.PNG

  2. ↓でいけるはず(-dを省略した場合はUsernameと同じ名前のDBとして認識されるらしい)

     $ psql -h [RDSのエンドポイント] -U [RDSのUsername] -d[RDSのDB NAME]
    
  3. cliで"[ec2-user@ip---- ~]"が"postgres=>"になっていれば接続成功

*接続しようとしたときにエラーが出るときは、

$ psql --help

でオプションを確認する(実際にネットで調べたものが間違っていて、2,3回やり直しました)

おまけ

  • RDSから抜ける

      $ \q
    
  • DBにログインする

      $ \c [DB名]
    
  • DB一覧の確認

      $ \l
    
  • DB内のテーブル一覧の確認(ログイン時)

      $ \z
    
40
40
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
40
40

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?