PostgreSQLをmacOS Sierra 10.12にインストールする

#環境
Homebrewがインストールされている

#Homebrewでインストールする
```terminal
<コンピューター名>-no-MBP:~ <ユーザー名>$ brew install postgresql
==> Installing dependencies for postgresql: icu4c, openssl, readline
==> Installing postgresql dependency: icu4c
==> Downloading https://homebrew.bintray.com/bottles/icu4c-64.2.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring icu4c-64.2.sierra.bottle.tar.gz
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local,
because macOS provides libicucore.dylib (but nothing else).

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

For compilers to find this software you may need to set:
    LDFLAGS:  -L/usr/local/opt/icu4c/lib
    CPPFLAGS: -I/usr/local/opt/icu4c/include
For pkg-config to find this software you may need to set:
    PKG_CONFIG_PATH: /usr/local/opt/icu4c/lib/pkgconfig

==> Summary
🍺  /usr/local/Cellar/icu4c/64.2: 257 files, 69.5MB
==> Installing postgresql dependency: openssl
==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2r.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring openssl-1.0.2r.sierra.bottle.tar.gz
==> Caveats
A CA file has been bootstrapped using certificates from the SystemRoots
keychain. To add additional certificates (e.g. the certificates added in
the System keychain), place .pem files in
  /usr/local/etc/openssl/certs

and run
  /usr/local/opt/openssl/bin/c_rehash

This formula is keg-only, which means it was not symlinked into /usr/local,
because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries.

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

For compilers to find this software you may need to set:
    LDFLAGS:  -L/usr/local/opt/openssl/lib
    CPPFLAGS: -I/usr/local/opt/openssl/include
For pkg-config to find this software you may need to set:
    PKG_CONFIG_PATH: /usr/local/opt/openssl/lib/pkgconfig

==> Summary
🍺  /usr/local/Cellar/openssl/1.0.2r: 1,795 files, 12.4MB
==> Installing postgresql dependency: readline
==> Downloading https://homebrew.bintray.com/bottles/readline-8.0.0_1.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring readline-8.0.0_1.sierra.bottle.tar.gz
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local,
because macOS provides the BSD libedit library, which shadows libreadline.
In order to prevent conflicts when programs look for libreadline we are
defaulting this GNU Readline installation to keg-only.

For compilers to find this software you may need to set:
    LDFLAGS:  -L/usr/local/opt/readline/lib
    CPPFLAGS: -I/usr/local/opt/readline/include
For pkg-config to find this software you may need to set:
    PKG_CONFIG_PATH: /usr/local/opt/readline/lib/pkgconfig

==> Summary
🍺  /usr/local/Cellar/readline/8.0.0_1: 48 files, 1.5MB
==> Installing postgresql
==> Downloading https://homebrew.bintray.com/bottles/postgresql-11.2_1.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring postgresql-11.2_1.sierra.bottle.tar.gz
==> /usr/local/Cellar/postgresql/11.2_1/bin/initdb --locale=C -E UTF-8 /usr/local/var/postgres
==> Caveats
To migrate existing data from a previous major version of PostgreSQL run:
  brew postgresql-upgrade-database

To have launchd start postgresql now and restart at login:
  brew services start postgresql
Or, if you don't want/need a background service you can just run:
  pg_ctl -D /usr/local/var/postgres start
==> Summary
🍺  /usr/local/Cellar/postgresql/11.2_1: 3,186 files, 35.5MB
==> Caveats
==> icu4c
This formula is keg-only, which means it was not symlinked into /usr/local,
because macOS provides libicucore.dylib (but nothing else).

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

For compilers to find this software you may need to set:
    LDFLAGS:  -L/usr/local/opt/icu4c/lib
    CPPFLAGS: -I/usr/local/opt/icu4c/include
For pkg-config to find this software you may need to set:
    PKG_CONFIG_PATH: /usr/local/opt/icu4c/lib/pkgconfig

==> openssl
A CA file has been bootstrapped using certificates from the SystemRoots
keychain. To add additional certificates (e.g. the certificates added in
the System keychain), place .pem files in
  /usr/local/etc/openssl/certs

and run
  /usr/local/opt/openssl/bin/c_rehash

This formula is keg-only, which means it was not symlinked into /usr/local,
because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries.

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

For compilers to find this software you may need to set:
    LDFLAGS:  -L/usr/local/opt/openssl/lib
    CPPFLAGS: -I/usr/local/opt/openssl/include
For pkg-config to find this software you may need to set:
    PKG_CONFIG_PATH: /usr/local/opt/openssl/lib/pkgconfig

==> readline
This formula is keg-only, which means it was not symlinked into /usr/local,
because macOS provides the BSD libedit library, which shadows libreadline.
In order to prevent conflicts when programs look for libreadline we are
defaulting this GNU Readline installation to keg-only.

For compilers to find this software you may need to set:
    LDFLAGS:  -L/usr/local/opt/readline/lib
    CPPFLAGS: -I/usr/local/opt/readline/include
For pkg-config to find this software you may need to set:
    PKG_CONFIG_PATH: /usr/local/opt/readline/lib/pkgconfig

==> postgresql
To migrate existing data from a previous major version of PostgreSQL run:
  brew postgresql-upgrade-database

To have launchd start postgresql now and restart at login:
  brew services start postgresql
Or, if you don't want/need a background service you can just run:
  pg_ctl -D /usr/local/var/postgres start
```
#バージョンを表示する
```terminal
<コンピューター名>-no-MBP:~ <ユーザー名>$ psql --version
psql (PostgreSQL) 11.2
```
#initdbでPostgreSQLのデータベースクラスタを新しく作成します
データベースクラスタとは、1つのサーバインスタンスで管理されるデータベースの集合です。
（https://www.postgresql.jp/document/9.1/html/app-initdb.html）

```terminal
<コンピューター名>-no-MBP:~ <ユーザー名>$ initdb /usr/local/var/postgres/ -E utf8
The files belonging to this database system will be owned by user "xxxxxx".
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".
```
#serviceで起動する
```terminal
<コンピューター名>-no-MBP:~ <ユーザー名>$ brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
```

#データベースの一覧を表示する
```terminal
<コンピューター名>-no-MBP:~ <ユーザー名>$ psql -l
                             List of databases
   Name    |  Owner   | Encoding | Collate | Ctype |   Access privileges   
-----------+----------+----------+---------+-------+-----------------------
 postgres  | xxxxxx   | UTF8     | C       | C     | 
 template0 | xxxxxx   | UTF8     | C       | C     | =c/xxxxxx         +
           |          |          |         |       | xxxxxx=CTc/xxxxxx
 template1 | xxxxxx   | UTF8     | C       | C     | =c/xxxxxx         +
           |          |          |         |       | xxxxxx=CTc/xxxxxx
(3 rows)
```

#ユーザーを指定してデータベースに入る
```
<コンピューター名>-no-MBP:~ <ユーザー名>$ psql -U <ユーザー名> postgres
psql (11.2)
Type "help" for help.
```

#データベースに入った状態でデータベースの一覧を表示する
```terminal
postgres=# \l
                             List of databases
   Name    |  Owner   | Encoding | Collate | Ctype |   Access privileges   
-----------+----------+----------+---------+-------+-----------------------
 postgres  | xxxxxx   | UTF8     | C       | C     | 
 template0 | xxxxxx   | UTF8     | C       | C     | =c/xxxxxx         +
           |          |          |         |       | xxxxxx=CTc/xxxxxx
 template1 | xxxxxx   | UTF8     | C       | C     | =c/xxxxxx         +
           |          |          |         |       | xxxxxx=CTc/xxxxxx
(3 rows)
```



#参考
https://qiita.com/uhooi/items/cd6256a72c6a03621c09