LoginSignup
68
66

More than 5 years have passed since last update.

OS X に PostgreSQL を Homebrew でインストールして brew services で起動する

Posted at

OSS-DB 技術者認定試験の勉強で PostgreSQL を Mac のローカルにインストールすることにしたのでその方法をメモっとく。

大したこと書いてないんだけど brew services というので起動できるということを知ったので記事にした。

PostgreSQL を Homebrew でインストールする

Homebrew を最新にしてから PostgreSQL をインストールする。

$ brew update
$ brew install postgresql

PostgreSQL を起動する

この記事では Homebrew のサービス管理機能の Homebrew/homebrew-services を使用する。

PostgreSQL は brew services start postgresql で起動できる。

$ brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)

brew services を利用することで OS X の launchctl で plist を登録したり、登録解除したりしてた作業が簡単になる。launchctl に登録されるので明示的に停止しないと Mac を起動するたびに PostgreSQL が自動起動する点は注意してほしい。

また、brew services list で起動状況が確認できる。蛇足だけど出力結果に出ているように MySQL もこれで管理できるよ。

$ brew services list
Name       Status  User   Plist
mysql      stopped        
postgresql started takuya /Users/takuya/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

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

psql -l で PostgreSQL に接続できて、データベース一覧が取得できることを確認しよう。

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

PostgreSQL を停止する

予想はつくと思うけど、PostgreSQL を brew services stop postgresql で停止できる。

$ brew services stop postgresql
Stopping `postgresql`... (might take a while)
==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)

参考文献

Homebrew/homebrew-services

68
66
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
68
66