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 --versionpsql (PostgreSQL) 9.1.9contains support for command-line editing-----(ここまで)-----
しかしこれではなくて、いつのまにか(?)MacPortsで導入されていた9.2系を用いる。
【"port installed"でMacPorts既導入物の確認】-----(ここから)-----$ port installed | grep postWarning: port definitions are more than two weeks old, consider using selfupdatepostgresql92 @9.2.3_0postgresql92 @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 --versionpsql (PostgreSQL) 9.2.4-----(ここまで)-----
クライアントツールは導入されていたが、サーバーは未導入だったので、"postgresql92-server"を追加導入した。
$ sudo port install postgresql92-server
【"port installed"で確認】-----(ここから)-----
$ port installed | grep postWarning: port definitions are more than two weeks old, consider using selfupdatepostgresql92 @9.2.3_0postgresql92 @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 dosudo mkdir -p /opt/local/var/db/postgresql92/defaultdbsudo chown postgres:postgres /opt/local/var/db/postgresql92/defaultdbsudo 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".Encoding "SJIS" is not allowed as a server-side encoding.Rerun initdb with a different locale selection.-----(ここまで)-----
"initdb"実行したら、「"SJIS"はunsupportedで使用不可」と怒られた。
$ 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 ... okcreating subdirectories ... okselecting default max_connections ... 20selecting default shared_buffers ... 1600kB・・・(略)・・・copying template1 to postgres ... okWARNING: enabling "trust" authentication for local connectionsYou 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/defaultdbor/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 startedLOG: 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 requestLOG: autovacuum launcher shutting downLOG: shutting downLOG: database system is shut downdoneserver stopped-----(ここまで)-----
次の作業の為、再びサーバーを始動した。
■ アプリケーション用のDBとユーザーの作成
まず、(デフォルトの)"postgres"ユーザーでログインし、"foo"ユーザーを作成。
$ createuser -U postgres -d -s -W foo
-d ... 新規DB作成権を付与-s ... superuser-W ... パスワード設定プロンプト
次に、"foo"ユーザーで、"bar"データーベースを作成。
$ createdb bar -U foo
確認する。
$ psql -U postgres -c '\l'-----(ここから)-----$ psql -U postgres -c '\l'List of databasesName | Owner | Encoding | Collate | Ctype | Access privileges-----------+----------+----------+-------------+-------------+-----------------------| | | | | postgres=CTc/postgres| | | | | postgres=CTc/postgres(4 rows)-----(ここまで)-----
◆以上