tstomoki.com

好きなことを好きなように

プログラミング

Homebrewを使ったPostgreSQLのインストール(Mac OS El Capitan)

2017/08/19

数年前にQiitaに投稿した記事が結構閲覧されているようなので、最新のMacOS El Capitan用に書き直してみました。
cf. Homebrewを使ったPostgreSQLのインストール(Mac OS Lion)

環境: Mac OS X El Capitan, OS 10.11.6

HomebrewでPostgreSQLのインストール

$ brew install postgresql

データベースの初期化 (文字コードはUTF-8)

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

なんか怒られたので言われる通りディレクトリ内を削除してinitdbをもう一度

$ initdb /usr/local/var/postgres -E utf8                                                                                                                                                                                                                      
The files belonging to this database system will be owned by user "tstomoki".

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".
$ rm -rf /usr/local/var/postgres/*                                                                                                                                                                                                                            
zsh: sure you want to delete all the files in /usr/local/var/postgres [yn]? y
$ initdb /usr/local/var/postgres -E utf8                                                                                                                                                                                                                      
The files belonging to this database system will be owned by user "tstomoki".
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.

fixing permissions on existing directory /usr/local/var/postgres ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
creating template1 database in /usr/local/var/postgres/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating collations ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
loading PL/pgSQL server-side language ... ok
vacuuming database template1 ... ok
copying template1 to template0 ... ok
copying template1 to postgres ... ok
syncing data to disk ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    pg_ctl -D /usr/local/var/postgres -l logfile start

PostgreSQLサーバの起動

$ pg_ctl -D /usr/local/var/postgres -l logfile start                                                                                                                                                                                                          
server starting
$ psql -l                                                                                                                                                                                                                                                     
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | tstomoki | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 |
 template0 | tstomoki | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/tstomoki          +
           |          |          |             |             | tstomoki=CTc/tstomoki
 template1 | tstomoki | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/tstomoki          +
           |          |          |             |             | tstomoki=CTc/tstomoki
(3 rows)

データベース一覧が取得出来ればインストール成功

PostgreSQLサーバの起動

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

PostgreSQLサーバの終了

$ pg_ctl -D /usr/local/var/postgres stop -s -m fast

PostgreSQLの自動起動設定

自動起動リストにPostgreSQLの追加

$ cp /usr/local/Cellar/postgresql/9.5.3/homebrew.mxcl.postgresql.plist ~/Library/LaunchAgents/

自動起動リストの設定ファイルの読み込み

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

コマンドライン上でのPostgreSQLの起動とコマンド

起動

$ psql 'database名'

終了(プロンプト上)

$ \q

データベース作成

$ createdb 'database名'

データベース一覧表示

$ psql -l

-プログラミング