Irohabook
0
1123

MacにPostgreSQLをHomebrewでインストールする:データベースの初期化(UTF8)とPostgreSQLの起動まで

Macに最初から入っているパッケージが多く、後から入れるパッケージを混在させてはいけない。追加したパッケージのバージョンを自分の都合で上げていくと、本体に影響する可能性もあるだろう(※1)。

マシン本体が使うパッケージと、こちらが使うパッケージを分離して、好きなようにパッケージをインストールとアインストールをできるようにした管理ツールがHomebrewである。Macの開発者の多くはHomebrewをインストールしている。

※1 認識に誤りがありましたらご指摘ください。

PostgreSQLのインストール

今回はそのHomebrewを使ってPostgreSQLをインストールする。

brew install postgresql

これで終わり。インストールは自動的に始まり、回線の速度が遅くなければ20秒ほどで終わる。

PostgreSQLのバージョン確認

postgres --version

下のように出る。

postgres (PostgreSQL) 10.4

PostgreSQLのインストール展開先

どこにインストールされたか確認してみよう。

cd /usr/local/var/

でvarディレクトリに移動する。ここでlsコマンドでファイル一覧を見ると

homebrew
log
mysql
postgres
run

となる。postgresがきちんと入っている。

PostgreSQLの初期化(utf8の設定)

PostgreSQLを使うには、文字コードをutf8にするといった初期化を最初に行う。

initdb /usr/local/var/postgres/ -E utf8

と入力すると、次のメッセージが出る。警告されているような気がするが、問題はない。

The files belonging to this database system will be owned by user "iroha".
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".

PostgreSQLの起動

brewコマンドを通してPostgreSQLを起動する。起動しないとデータベースにアクセスできない。何かにつまづいている人はここをスキップしている可能性がある。PostgreSQLの起動は以下のコマンド。

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.
Unpacking objects: 100% (14/14), done.
remote: Total 14 (delta 0), reused 8 (delta 0), pack-reused 0
Tapped 0 formulae (43 files, 55.3KB)
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)

終わったらようやくデータベースを確認できる。

PostgreSQLのデータベースを表示する

初期状態で3つのデータベースが入っている。これらを表示するには

psql -l

というコマンドを打つ。すると次のような表が出てくる。

                              List of databases
   Name    | Owner | Encoding |   Collate   |    Ctype    | Access privileges 
-----------+-------+----------+-------------+-------------+-------------------
 postgres  | iroha | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | iroha | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/iroha         +
           |       |          |             |             | iroha=CTc/iroha
 template1 | iroha | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/iroha         +
           |       |          |             |             | iroha=CTc/iroha

データベースが表示された。Ownerにあるirohaは私のMacのユーザー名である。起動する前にデータベースの初期化でutf8を設定しているので、Encodingがutf8になっている。

次の記事

Homebrew