homebrewでpostgresqlを設置する。


## brewでpostgresqlを確認

* postgresqlの設置情報を確認してみると、まだ設置されていないし、依存関係にあるlibraryも表示されます。

  ```sh
    [devnote@hooni:~] % brew info postgresql
    postgresql: stable 9.3.5 (bottled), devel 9.4rc1
    http://www.postgresql.org/
    Conflicts with: postgres-xc
    Not installed
    From: https://github.com/Homebrew/homebrew/blob/master/Library/Formula/postgresql.rb
    ==> Dependencies
    Required: openssl ✔, readline ✔
    Recommended: ossp-uuid ✘
    ==> Options
    --32-bit
      Build 32-bit only
    --enable-dtrace
      Build with DTrace support
    --no-perl
      Build without Perl support
    --no-tcl
      Build without Tcl support
    --with-python
      Build with python support
    --without-ossp-uuid
      Build without ossp-uuid support
    --devel
      install development version 9.4rc1
    ==> Caveats
    If builds of PostgreSQL 9 are failing and you have version 8.x installed,
    you may need to remove the previous version first. See:
      https://github.com/Homebrew/homebrew/issues/issue/2510

    To migrate existing data from a previous major version (pre-9.3) of PostgreSQL, see:
      http://www.postgresql.org/docs/9.3/static/upgrading.html

    When installing the postgres gem, including ARCHFLAGS is recommended:
      ARCHFLAGS="-arch x86_64" gem install pg

    To install gems without sudo, see the Homebrew documentation:
    https://github.com/Homebrew/homebrew/blob/master/share/doc/homebrew/Gems,-Eggs-and-Perl-Modules.md

    To have launchd start postgresql at login:
        ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
    Then to load postgresql now:
        launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
    Or, if you don't want/need launchctl, you can just run:
        postgres -D /usr/local/var/postgres
  ```

## brewでpostgresqlを設置

* homebrewで設置すると依存関係にあるlibraryも一緒に設置されます。

  ```sh
    [devnote@hooni:~] % brew install postgresql
    ==> Installing postgresql dependency: ossp-uuid
    ==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/ossp-uuid-1.6.2_1.yosemite.bottle.tar.gz
    ######################################################################## 100.0%
    ==> Pouring ossp-uuid-1.6.2_1.yosemite.bottle.tar.gz
    🍺  /usr/local/Cellar/ossp-uuid/1.6.2_1: 16 files, 256K
    ==> Installing postgresql
    ==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/postgresql-9.3.5_1.yosemite.bottle.1.tar.gz
    ######################################################################## 100.0%
    ==> Pouring postgresql-9.3.5_1.yosemite.bottle.1.tar.gz
    ==> Caveats
    If builds of PostgreSQL 9 are failing and you have version 8.x installed,
    you may need to remove the previous version first. See:
      https://github.com/Homebrew/homebrew/issues/issue/2510

    To migrate existing data from a previous major version (pre-9.3) of PostgreSQL, see:
      http://www.postgresql.org/docs/9.3/static/upgrading.html

    When installing the postgres gem, including ARCHFLAGS is recommended:
      ARCHFLAGS="-arch x86_64" gem install pg

    To install gems without sudo, see the Homebrew documentation:
    https://github.com/Homebrew/homebrew/blob/master/share/doc/homebrew/Gems,-Eggs-and-Perl-Modules.md

    To have launchd start postgresql at login:
        ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
    Then to load postgresql now:
        launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
    Or, if you don't want/need launchctl, you can just run:
        postgres -D /usr/local/var/postgres
    ==> /usr/local/Cellar/postgresql/9.3.5_1/bin/initdb /usr/local/var/postgres
    ==> Summary
    🍺  /usr/local/Cellar/postgresql/9.3.5_1: 2927 files, 38M 
  ```

## データベースを初期化

* initdbを実行します。実行したことがある場合はそのまま使うか、生成されたデータベースを削除してもう一度initdbを実行します。

  ```sh
    [devnote@hooni:~] % initdb /usr/local/var/postgres -E utf8
    The files belonging to this database system will be owned by user "devnote".
    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/postgres" exists but is not empty
    If you want to create a new database system, either remove or empty
    the directory "/usr/local/var/postgres" or run initdb
    with an argument other than "/usr/local/var/postgres".
  ```

## launchdで起動してデータベースを確認

* 設置するとき書かれていた通りにsymbolic linkを生成して、launchdで起動します。

  ```sh
    [devnote@hooni:~] % ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
    /Users/devnote/Library/LaunchAgents/homebrew.mxcl.postgresql.plist -> /usr/local/opt/postgresql/homebrew.mxcl.postgresql.plist
    [devnote@hooni:~] % launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
  ```

* データベース一覧を確認します。

  ```sh
    [devnote@hooni:~] % psql -l
                                    List of databases
       Name    |  Owner  | Encoding |   Collate   |    Ctype    |  Access privileges
    -----------+---------+----------+-------------+-------------+---------------------
     postgres  | devnote | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 |
     template0 | devnote | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/devnote         +
               |         |          |             |             | devnote=CTc/devnote
     template1 | devnote | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/devnote         +
               |         |          |             |             | devnote=CTc/devnote
    (3 rows)
  ```