ECのウェブ担当者のメモ

ECサイトを運営管理している、WEB担当プログラマのメモ

スポンサーリンク

MacにPostgresをサクッとインストール&初期設定

f:id:jun9632:20160226105945p:plain

MaxにPostgresをhomebrewを使ってインストールして初期設定までの手順です。

環境

今回、インストールを行った環境は以下です。

Max OS X El Capitan
Postgres 9.4.5

Postgresのインストール

homebrewを使ってインストールします。

brew install postgresql

もし、元々Postgresがインストール済みの場合は、上記の作業は不要です。

データベースの文字コードの設定

インストールが完了したら、文字コードをutf-8に設定します。

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

サーバーの起動

以下の実行して、postgresサーバーを起動します。

postgres -D /usr/local/var/postgres

起動の確認

サーバーが起動したら以下のpsqlコマンド実行します。

psql -l

以下のように、データベースの一覧が取得できればOKです。

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

環境変数の設定

Postgresで使用するデータをどこに格納するかを指定するため、PGDATA を設定するため、.bash_profileに以下を追加します。

export PGDATA=/usr/local/var/postgres

上記を追加したら、bash_profileを再読み込みさせます。

source .bash_profile

自動起動の設定

起動時にpostgresを起動実行するように、以下を実行します。

cp /usr/local/Cellar/postgresql/9.4.5_2/homebrew.mxcl.postgresql.plist ~/Library/LaunchAgents/

Postgresのバージョンにより、パスが変わるので、適宜環境に合わせて調整してください。

手動起動/停止の場合

起動

手動でpostgresを起動する場合は、以下のコマンド実行する

pg_ctl -l /usr/local/var/postgres/server.log start

停止

手動でpostgresを停止する場合は、以下のコマンド実行する

pg_ctl stop

以上、結構簡単にインストールと設定が完了します。

関連記事

marketing-web.hatenablog.com