akiyoko blog

akiyoko の IT技術系ブログです

Mac の MySQL クライアントに「Sequel Pro」を使っているなら PostgreSQL クライアントは「PSequel」がオススメ

タイトル通りですが、Mac の MySQL クライアントに「Sequel Pro」を使っているなのであれば、PostgreSQL クライアントは「PSequel」がオススメです。


長年、Mac の PostgreSQL クライアントに不満があり、使い勝手の良いアプリを探し求めていたのですが、ついにその答えを見つけたような気がします。

私が個人的に一番使い勝手が良いと思っている MySQL クライアントが「Sequel Pro」なのですが、その Sequel Pro の PostgreSQL 版とも言うべきアプリがこの「PSequel」なのです。


PSequel の作者も、このように言っています。

Well, pgAdmin is great for its feature-richness. However, I found its UI is clumsy and complicated. I know there is a list of PostgreSQL GUI Tools. However, they are either web-based, Java-based* or don't support the features I want. In the good old MySQL world, my favorite client is Sequel Pro, but its support for PostgreSQL doesn't seem to be happening. So, I decided to make one myself.


pgAdmin って機能が抱負で良いんだけどさ、UI がイケてないよね。他にも PostgreSQL の GUI ツールはいろいろあるんだけど、Web ベースだったり、Java ベースだったり、欲しい機能が無かったりでちょっとアレだよね。MySQL だったら、僕のお気に入りのクライアントは Sequel Pro なんだけど、どうやら PostgreSQL のサポートはしなさそうだし、じゃあ自分で作っちゃえって思っちゃったわけ。


(akiyoko 意訳)


 
さて今回は、PSequel の使い方、特に PostgreSQL への接続方法について紹介します。


そもそも実際のシステムでは、PostgreSQL 等のデータベースは通常、サーバの外にはポートは開放しておらず、SSH でリモートサーバに乗り込んでサーバ上のデータベースに接続するというパターンが多いでしょう。本番環境や検証環境では、公開鍵認証でログインすることが一般的でしょうが、開発環境では Vagrant 等で仮想サーバを利用することも多く、その場合はパスワード認証を使うこともあるでしょう。


そこで今回は、リモートサーバ上の PostgreSQL に SSH トンネル経由で接続する方法として、

  • 1)サーバに SSH公開鍵認証でログイン ⇒ サーバ上の PostgreSQL に接続
  • 2)サーバにパスワード認証でログイン ⇒ サーバ上の PostgreSQL に接続

という二つのパターンを紹介します *1


接続する PostgreSQL の設定は、以下の通りとします。

項目 設定内容
データベース名 myproject
データベースユーザ myprojectuser
データベースユーザパスワード myprojectuserpass


 

インストール

インストールするには、Homebrew で

$ brew cask install psequel

とするか、公式ページ から app ファイルをダウンロードしてインストールします。


www.psequel.com


 

PostgreSQL への接続

1)サーバに SSH公開鍵認証ログイン ⇒ サーバ上の PostgreSQL に接続

サーバに SSH公開鍵認証でログインして、SSHトンネル経由で PostgreSQL に接続する方法です。

本番環境(PostgreSQL on EC2)として、各種設定は以下を想定しています。

サーバ IPアドレス 52.100.100.100
ログインユーザ ubuntu
SSH秘密鍵 ~/.ssh/aws_p1.pem


f:id:akiyoko:20160729012558p:plain


2)サーバにパスワード認証ログイン ⇒ サーバ上の PostgreSQL に接続

サーバに SSH公開鍵認証でログインして、SSHトンネル経由で PostgreSQL に接続する方法です。

開発環境(PostgreSQL on Vagrant)として、各種設定は以下を想定しています。

サーバ IPアドレス 192.168.32.10
ログインユーザ vagrant
ログインパスワード vagrant


しかしながら、ここでハマりポイントがあります。
実は、PSequel はパスワード認証による SSHトンネリング機能をサポートしていないため(そして今後もサポートする予定は無さそう *2)、Vagrant のセッティングから SSH秘密鍵のパスを参照することができるという裏ワザ(?)を利用します。

The biggest requirement for a GUI of any sort (whether it is for a database client or something else), is the ability to use SSH tunneling to connect to the server. SSH tunneling allows an application to first connect to a machine (in this case a virtual machine managed by Vagrant), and act as normally as if it were connected directly to a service on your personal machine.


While PSequel does support SSH tunneling, what it does not do is support password authentication. I’m sure there was a good reason for this, but in the case of Vagrant it is irritating nontheless. Most Vagrant boxes have a default username and password (vagrant and vagrant respectively). This makes it incredibly simple to use applications that support SSH tunneling.


In the case of PSequel, the only authentication method that is supported is key authentication. Luckily for us, Vagrant does typically utilize this method of authentication (when you SSH into the Vagrant machine, for example, a private key is used so you don’t have to enter a password). Unfortunately for us, the location of this private key can be different for every machine. If you don’t know how to find it, using PSequel can seem impossible.


To find the private key for a Vagrant box, open up a terminal, navigate to the project folder, and type vagrant ssh-config. You’ll see something like this:


http://zacharyflower.com/using-psequel-with-vagrant/

操作は簡単で、Vagrant のホームに移動して「vagrant ssh-config」コマンドを実行すれば OK です。

$ cd vagrant/ubuntu14/
$ vagrant ssh-config
Host default
  HostName 127.0.0.1
  User vagrant
  Port 2200
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /Users/akiyoko/vagrant/ubuntu14/.vagrant/machines/default/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL


 
これを利用して、以下の接続設定を行えばよいでしょう。

f:id:akiyoko:20160729012628p:plain

f:id:akiyoko:20160729012651p:plain


ただし正確に言えば、パスワード認証でサーバにログインできない問題が解決できたわけではないので、Vagrant 以外だったらどうするか?については今後の課題でしょうか。




ここからは、MySQL クライアントの「Sequel Pro」*3 の接続設定についてのメモです。

「Sequel Pro」の接続設定

1)サーバに SSH公開鍵認証ログイン ⇒ サーバ上の PostgreSQL に接続

本番環境(PostgreSQL on EC2)として、各種設定は以下を想定。

サーバ IPアドレス 52.100.100.100
ログインユーザ ubuntu
SSH秘密鍵 ~/.ssh/aws_p1.pem

f:id:akiyoko:20160729012257p:plain

2)サーバにパスワード認証ログイン ⇒ サーバ上の PostgreSQL に接続

開発環境(PostgreSQL on Vagrant)として、各種設定は以下を想定。

サーバ IPアドレス 192.168.32.10
ログインユーザ vagrant
ログインパスワード vagrant

f:id:akiyoko:20160729012527p:plain

*1:2015年1月より、PSequel に SSH トンネリング機能がサポートされました。https://twitter.com/psequel/status/550881414540173312

*2:https://github.com/psequel/psequel/issues/17

*3:ちなみに、Sequel は「シクォル」と発音するようです。