たなかこういちの資料室

システム開発に携わる筆者があれこれ試したことや学んだことについてのまとめ

Postgresセットアップの記録(on My Mac)

2013年10月29日時点のMy MacのAs-Is状態に対しての、Postgres導入・初期設定実施記録です。
 
前提
 
- OS X 10.8.5
- 2013年10月29日に実施
 
既導入物の確認とシンボリックリンク作成、および"postgresql92-server"の追加導入
 
初期状態では下記のようになっていた。
 
【"psql"コマンドの所在とバージョンの確認】
-----(ここから)-----
$ which psql
/usr/bin/psql
$ psql --version
contains support for command-line editing
-----(ここまで)-----
 
しかしこれではなくて、いつのまにか(?)MacPortsで導入されていた9.2系を用いる。
 
【"port installed"でMacPorts既導入物の確認】
-----(ここから)-----
$ port installed | grep post
Warning: port definitions are more than two weeks old, consider using selfupdate
  postgresql92 @9.2.3_0
  postgresql92 @9.2.4_0 (active)
  postgresql_select @0.1_0 (active)
-----(ここまで)-----
 
【導入されていたパス】
/opt/local/lib/postgresql92
 
"/opt/local/lib/postgresql92/bin"下のコマンド(※psql, pg_ctl, createdb, createuser, etc.)のシンボリックリンクを"/opt/local/bin"下に作成した。これで、環境変数PATHの設定は下記の通りなので、"/usr/bin/psql"等ではなく"/opt/local/bin/psql"等が実行されるようになった。
 
PATH=/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
 
【"psql"コマンドの所在とバージョンの確認(※シンボリックリンク作成後)】
-----(ここから)-----
$ which psql
/opt/local/bin/psql
$ psql --version
-----(ここまで)-----
 
※一度"/usr/bin/psql"を実行してしまった後では、上記のようにシンボリックリンクを作成しても、"/opt/local/bin/psql"の方が実行されず、変わらず"/usr/bin/psql"の方が実行されるような様子があったため、念のためターミナルは再起動した。
 
クライアントツールは導入されていたが、サーバーは未導入だったので、"postgresql92-server"を追加導入した。
 
$ sudo port install postgresql92-server
 
【"port installed"で確認】
-----(ここから)-----
$ port installed | grep post
Warning: port definitions are more than two weeks old, consider using selfupdate
  postgresql92 @9.2.3_0
  postgresql92 @9.2.4_0 (active)
  postgresql92-server @9.2.4_0 (active)
  postgresql_select @0.1_0 (active)
-----(ここまで)-----
 
"defaultdb"初期構築("initdb"の実施)
 
"postgresql92-server"の"port install"を実行したとき、コンソールに下記のようなメッセージが出る。
 
-----(ここから)-----
$ sudo port install postgresql92-server
Warning: port definitions are more than two weeks old, consider using selfupdate
--->  Computing dependencies for postgresql92-server
・・・(略)・・・
 
To create a database instance, after install do
 sudo mkdir -p /opt/local/var/db/postgresql92/defaultdb
 sudo chown postgres:postgres /opt/local/var/db/postgresql92/defaultdb
 sudo su postgres -c '/opt/local/lib/postgresql92/bin/initdb -D /opt/local/var/db/postgresql92/defaultdb'
・・・(略)・・・ 
-----(ここまで)-----
 
「To create a database instance, after install do」下に示される3つのコマンドを実行した。"defaultdb"のパスは少しだけアレンジした。
 
$ sudo mkdir -p /opt/local/var/postgresql92/defaultdb
$ sudo chown postgres:postgres /opt/local/var/postgresql92/defaultdb
 
※ユーザー"postgres"は、"port install postgresql92-server"で作成された?
 
$ sudo su postgres -c '/opt/local/lib/postgresql92/bin/initdb -D /opt/local/var/postgresql92/defaultdb'
 
【"initdb"実行結果】
-----(ここから)-----
$ sudo su postgres -c '/opt/local/lib/postgresql92/bin/initdb -D /opt/local/var/postgresql92/defaultdb'
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
 
The database cluster will be initialized with locale "ja_JP.SJIS".
initdb: locale "ja_JP.SJIS" requires unsupported encoding "SJIS"
Encoding "SJIS" is not allowed as a server-side encoding.
Rerun initdb with a different locale selection.
-----(ここまで)-----
 
"initdb"実行したら、「"SJIS"はunsupportedで使用不可」と怒られた。
下記のように"locale"を指定して再実行を試みた。(※"-E UTF-8"オプションによるエンコーディング指定では「ロケールの使用するSJISと合わない」と怒られた。)
 
$ sudo su postgres -c '/opt/local/lib/postgresql92/bin/initdb --locale=ja_JP.UTF-8 -D /opt/local/var/postgresql92/defaultdb'
 
【"initdb"実行結果】
-----(ここから)-----
$ sudo su postgres -c '/opt/local/lib/postgresql92/bin/initdb --locale=ja_JP.UTF-8 -D /opt/local/var/postgresql92/defaultdb'
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
 
The database cluster will be initialized with locale "ja_JP.UTF-8".
The default database encoding has accordingly been set to "UTF8".
initdb: could not find suitable text search configuration for locale "ja_JP.UTF-8"
The default text search configuration will be set to "simple".
 
fixing permissions on existing directory /opt/local/var/postgresql92/defaultdb ... ok
creating subdirectories ... ok
selecting default max_connections ... 20
selecting default shared_buffers ... 1600kB
・・・(略)・・・
copying template1 to postgres ... 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:
 
    /opt/local/lib/postgresql92/bin/postgres -D /opt/local/var/postgresql92/defaultdb
or
    /opt/local/lib/postgresql92/bin/pg_ctl -D /opt/local/var/postgresql92/defaultdb -l logfile start
 
-----(ここまで)-----
 
起動・停止
 
下記コマンドでサーバーを起動した。"postgres"ユーザーで実行する必要がある。
 
$ sudo su postgres -c '/opt/local/lib/postgresql92/bin/pg_ctl -D /opt/local/var/postgresql92/defaultdb start'
 
【"pg_ctl start"実行結果】
-----(ここから)-----
$ sudo su postgres -c '/opt/local/lib/postgresql92/bin/pg_ctl -D /opt/local/var/postgresql92/defaultdb start'
LOG:  database system was shut down at 2013-10-29 21:15:59 JST
LOG:  autovacuum launcher started
LOG:  database system is ready to accept connections
-----(ここまで)-----
 
停止した。
 
$ sudo su postgres -c '/opt/local/lib/postgresql92/bin/pg_ctl -D /opt/local/var/postgresql92/defaultdb stop' 
 
【"pg_ctl stop"実行結果】
-----(ここから)-----
$ sudo su postgres -c '/opt/local/lib/postgresql92/bin/pg_ctl -D /opt/local/var/postgresql92/defaultdb stop' 
waiting for server to shut down....
LOG:  received smart shutdown request
LOG:  autovacuum launcher shutting down
LOG:  shutting down
LOG:  database system is shut down
 done
server stopped
-----(ここまで)-----
 
次の作業の為、再びサーバーを始動した。
 
アプリケーション用のDBとユーザーの作成
 
まず、(デフォルトの)"postgres"ユーザーでログインし、"foo"ユーザーを作成。
 
$ createuser -U postgres -d  -s -W foo
 
-d ... 新規DB作成権を付与
-s ... superuser
-W ... パスワード設定プロンプト
 
※"createuser"、"createdb"、"psqlコマンドには、"/opt/local/bin"下(=Pathの通っているところ)にシンボリックリンクを作成している。
 
次に、"foo"ユーザーで、"bar"データーベースを作成。
 
$ createdb bar -U foo
 
確認する。
 
$ psql -U postgres -c '\l'
 
-----(ここから)-----
$ psql -U postgres -c '\l'
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges  
-----------+----------+----------+-------------+-------------+-----------------------
 bar       | foo      | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 |
 postgres  | postgres | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 |
 template0 | postgres | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(4 rows)
-----(ここまで)-----
 
なお、ターミナルの環境設定で「文字エンコーディング」が「日本語(Mac OS)」だと、"\"(半角バックスラッシュ)が正しく入力できなかった。「UnicodeUTF-8)」に設定したら入力できた。
 
◆以上