Mac OS High Sierra で Homebrew を使い、バージョンを指定して PostgreSQL をインストールする

Mac OS High Sierra で Homebrew を使い、バージョンを指定して PostgreSQL をインストールする

Homebrew を使えば MacPostgreSQL を簡単にインストールできるが、うっかりバージョンを指定せずに実行したのでやり直しにハマった話。

実行環境

Mac OS High Sierra (10.13.2)
MacBook Air Mid 2013
Homebrew 1.4.2

インストールの記録

Homebrew を使用してインストール

$ brew install postgresql

バージョンの確認

$ psql --version
psql (PostgreSQL) 10.1

2018年1月現在の最新版は version 10.1 の様だが、9.xをインストールしたかったのでやり直す事にする。
インストールできるバージョンの確認

$ brew search postgresql
==> Searching local taps...
postgresql ✔                  postgresql@9.4                postgresql@9.5                postgresql@9.6

チェックの入っているのが現在インストールされている version 10.1

version 10.1 をアンインストール

$ brew uninstall --force postgresql
Uninstalling postgresql... (3,372 files, 38.8MB)

設定ファイル等を削除

$ rm -rf /usr/local/var/postgres

version 9.6 を再インストール

バージョンを指定する為に homebrew versions を使用。

$ brew search brew tap homebrew/versions

インストール可能なバージョンを確認

$ brew search postgresql
==> Searching local taps...
postgresql                    postgresql@9.4                postgresql@9.5                postgresql@9.6

バージョンを指定してインストール

$ brew install postgresql@9.6
==> Downloading https://homebrew.bintray.com/bottles/postgresql@9.6-9.6.6.high_sierra.bottle.tar.gz  

(中略)  

If you need to have this software first in your PATH run:
  echo 'export PATH="/usr/local/opt/postgresql@9.6/bin:$PATH"' >> ~/.bash_profile

For compilers to find this software you may need to set:
    LDFLAGS:  -L/usr/local/opt/postgresql@9.6/lib
    CPPFLAGS: -I/usr/local/opt/postgresql@9.6/include


To have launchd start postgresql@9.6 now and restart at login:
  brew services start postgresql@9.6
Or, if you don't want/need a background service you can just run:
  pg_ctl -D /usr/local/var/postgresql@9.6 start
==> Summary
🍺  /usr/local/Cellar/postgresql@9.6/9.6.6: 3,273 files, 36.8MB

パスを通す

echo 'export PATH="/usr/local/opt/postgresql@9.6/bin:$PATH"' >> ~/.bash_profile

.bash_profile の読み込み

$ source .bash_profile

バージョンの確認

$ psql --version
psql (PostgreSQL) 9.6.6

データベースを初期化

$ initdb /usr/local/var/postgresql@9.6 -E utf8
The files belonging to this database system will be owned by user "ibeckuu".
This user must also own the server process.

The database cluster will be initialized with locale "ja_JP.UTF-8".
initdb: could not find suitable text search configuration for locale "ja_JP.UTF-8"
The default text search configuration will be set to "simple".

Data page checksums are disabled.

initdb: directory "/usr/local/var/postgresql@9.6" exists but is not empty
If you want to create a new database system, either remove or empty
the directory "/usr/local/var/postgresql@9.6" or run initdb
with an argument other than "/usr/local/var/postgresql@9.6".

結果的にはこの状態でインストールが出来ていたのだが、下記コマンドで起動しなかった為ディレクトリ名を変更したりシンボリックリンクを貼ってみたりして無茶苦茶になってしまった。

起動コマンド(最新版では問題なく使えるが、ディレクトリ名に'@~'が付加されているバージョンでは使える様にできなかった)

$ brew services start postgresql
==> Tapping homebrew/services
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-services'...
remote: Counting objects: 14, done.
remote: Compressing objects: 100% (10/10), done.
remote: Total 14 (delta 0), reused 9 (delta 0), pack-reused 0
Unpacking objects: 100% (14/14), done.
Tapped 0 formulae (43 files, 55.4KB)
Error: Formula `postgresql` is not installed.
# 最後でコケる

この後、何度もアンインストール、再インストールを繰り返したが、結局は上記の状態で良く、起動は下記コマンドを使用する。

$ postgres -D /usr/local/var/postgresql@9.6
LOG:  database system was shut down at 2018-01-08 21:25:55 JST
LOG:  MultiXact member wraparound protections are now enabled
LOG:  database system is ready to accept connections
LOG:  autovacuum launcher started

データベース一覧を取得

$ psql -l
                                List of databases
   Name    |  Owner  | Encoding |   Collate   |    Ctype    |  Access privileges  
-----------+---------+----------+-------------+-------------+---------------------
 postgres  | ibeckuu | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | ibeckuu | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/ibeckuu         +
           |         |          |             |             | ibeckuu=CTc/ibeckuu
 template1 | ibeckuu | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/ibeckuu         +
           |         |          |             |             | ibeckuu=CTc/ibeckuu
(3 rows)

データベースの置き場所の設定

.bash_profile に追加

$ echo 'export PGDATA=/usr/local/var/postgresql@9.6' >> ~/.bash_profile

ロールの追加

Homebrew でインストールするとインストールした$USERがスーパーユーザーになっており、あまり良くないので ロール(ユーザーとグループが一緒になった様なもの)を追加する。 スーパーユーザーとしてログインし、SQL CREATE ROLE を発行しても作成できるが今回はシェルから行う。

<参考>
PostgreSQL 9.6.5文書

# ユーザーを追加、 Pオプションでログイン時にパスワードを必要にする
$ createuser -P user1
Enter password for new role: 
Enter it again: 

作成されたユーザーを所有者とするデータベースを作成

# -O オプションで所有者を指定
$ createdb sample_list -O user1

データベース一覧を取得

$ psql -l
                                 List of databases
    Name     |  Owner  | Encoding |   Collate   |    Ctype    |  Access privileges  
-------------+---------+----------+-------------+-------------+---------------------
 postgres    | ibeckuu | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 sample_list | user1   | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0   | ibeckuu | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/ibeckuu         +
             |         |          |             |             | ibeckuu=CTc/ibeckuu
 template1   | ibeckuu | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/ibeckuu         +
             |         |          |             |             | ibeckuu=CTc/ibeckuu
(4 rows)

<参考サイト>
Macで複数のバージョンのPostgresqlを共存させる
[MacOS] Homebrew で PostgreSQL をインストールする手順
[MacOS] PostgreSQL の全バージョンをアンインストールする方法 ~ Homebrew 編

では、