LoginSignup
75
72

More than 5 years have passed since last update.

Mac OS X YosemiteにPostgreSQLをインストール

Last updated at Posted at 2015-04-16

概要

Homebrew を使って PostgreSQL を MacBook Air に導入した時のメモです。
必要最低限コレだけすれば大丈夫でしょ!ということだけ書いております。

手順

Homebrew でインストール

端末にbrew install postgresとぶち込みます。

~ $ brew install postgres
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/postgresql-9.4.0.yos
Already downloaded: /Library/Caches/Homebrew/postgresql-9.4.0.yosemite.bottle.tar.gz
==> Pouring postgresql-9.4.0.yosemite.bottle.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/2510

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

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
==> Summary
🍺  /usr/local/Cellar/postgresql/9.4.0: 2990 files, 40M

表示されたログのうち、注目すべきは以下の部分です。
簡単な設定方法がさらっと書いてあります。見逃しそうでした。

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

基本的にここに書いてあるコマンドを使うだけで最低限の設定はできます。

ログイン時の起動設定

基本的に常時起動していて欲しいので、先ほど出てきたコマンドのうち以下を実行します。

~ $ ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
/Users/<username>/Library/LaunchAgents/homebrew.mxcl.postgresql.plist -> /usr/local/opt/postgresql/homebrew.mxcl.postgresql.plist

launchd

突然出てきた LaunchAgents ディレクトリにビックリしたので簡単に調べました。

launchdはデーモン、アプリケーション、プロセス、スクリプトの起動・停止・管理を行う、オープンソースのサービス管理フレームワークである。アップルのDave Zarzyckiによって作られ、Mac OS X Tiger(Mac OS X v10.4)で導入された。Apache Licenseのもとで公開されている。

launchd - Wikipedia

要するに、Mac OS X 上で、各種サービスの起動・停止・管理を行ってくれる賢いやつらしいです。ログイン時に起動するように設定する(シンボリックリンクを貼る)のが先ほどのスクリプトだと思います。

起動する

すぐに使いたいので、lunchd に登録した PostgreSQL を明示的に叩き起こします。

~ $ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

終わり!もう完了!もう使えるよ!楽すぎでしょ!

試してみる

試してみます。

データベース一覧を表示

何があるのか気になったのでとりあえず表示してみました。

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

ログインして色々してみた

ログイン

~ $ psql postgres
psql (9.4.0)
Type "help" for help.

テーブル作成

postgres=# create table users (id int, username text, email text);
CREATE TABLE

postgres=# \d
         List of relations
 Schema | Name  | Type  |  Owner
--------+-------+-------+----------
 public | users | table |  hoge
(1 row)

postgres=# \d users
      Table "public.users"
  Column  |  Type   | Modifiers
----------+---------+-----------
 id       | integer |
 username | text    |
 email    | text    |

データ挿入して選択して削除

postgres=# insert into users (id, username, email) values (1, 'hoge', 'hoge@hoge.com');
INSERT 0 1

postgres=# select * from users;
 id | username |     email
----+----------+---------------
  1 | hoge     | hoge@hoge.com
(1 row)

postgres=# truncate table users;
TRUNCATE TABLE

postgres=# select * from users;
 id | username | email
----+----------+-------
(0 rows)

テーブル削除

postgres=# drop table users;
DROP TABLE
postgres=# \d
No relations found.

動いてました。めでたしめでたし。

まとめ

Mac OS X だと以下の3ステップで PostgreSQL は使える!

  1. brew install postgres
  2. ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
  3. launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

楽ちん。

75
72
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
75
72