MaxOSX10.5にPostgreSQLをインストールして色々できるまで
mac portからインストールする
% sudo port install postgresql83 postgresql83-server
自動起動するように
こうしなさいって、MacPortに言われる。
% sudo launchctl load -w /Library/LaunchDaemons/org.macports.postgresql83-server.plist
よく分かってない。
でも、このコマンドによって自動的にpostgresql83-serverが起動するようになる。
ログは以下に保存される。
/opt/local/var/log/postgresql83/postgres.log
MacPortに言われるがまま
ごにょ、ごにょ。
% sudo mkdir -p /opt/local/var/db/postgresql83/defaultdb % sudo chown postgres:postgres /opt/local/var/db/postgresql83/defaultdb % sudo su postgres -c '/opt/local/lib/postgresql83/bin/initdb -D /opt/local/var/db/postgresql83/defaultdb'
データベースサーバの起動
これも言われるがまま。
2つの方法があるよって、言われる。
% /opt/local/lib/postgresql83/bin/postgres -D /opt/local/var/db/postgresql83/defaultdb
% /opt/local/lib/postgresql83/bin/pg_ctl -D /opt/local/var/db/postgresql83/defaultdb -l logfile start
とりあえず、前者を実行しておく。
ユーザーpostgresで実行する必要がある。
% sudo su - postgres /opt/local/lib/postgresql83/bin/postgres -D /opt/local/var/db/postgresql83/defaultdb
終了するには
% sudo su postgres -c '/opt/local/lib/postgresql83/bin/pg_ctl -D /opt/local/var/db/postgresql83/defaultdb stop'
環境変数を設定する
シェルはzshを使っているので、.zshenvに以下のような記述をしておく。
# postgresql83 export LD_LIBRARY_PATH=/opt/local/lib/postgresql83 export PATH=$LD_LIBRARY_PATH/bin/:$PATH
他の値もあるようだが、今の状態はこんな感じ。
ユーザー追加
データベース管理者のpostgresだけだと、色々、面倒なので。
ユーザーを追加した。
% sudo su - postgres % /opt/local/lib/postgresql83/bin/createuser rdera Shall the new role be a superuser? (y/n) n Shall the new role be allowed to create databases? (y/n) y Shall the new role be allowed to create more new roles? (y/n) n
管理者postgresになってから、コマンドを実行する。
データベースを作ってみる
ユーザーrderaで
% createdb test_db
確認してみる。
% psql -l /Users/rdera List of databases Name | Owner | Encoding -----------+----------+---------- postgres | postgres | UTF8 template0 | postgres | UTF8 template1 | postgres | UTF8 test_db | rdera | UTF8 (4 rows)
わーい。
データベースの起動、作成
作ったデータベースtest_dbにアクセスする。
% psql test_db /Users/rdera Welcome to psql 8.3.5, the PostgreSQL interactive terminal. Type: \copyright for distribution terms \h for help with SQL commands \? for help with psql commands \g or terminate with semicolon to execute query \q to quit
データベースtest_dbに対するプロンプトが表示される。
test_db=>
テーブルを作る。
test_db=> create table test_table ( "Name" TEXT , "Number" INTEGER );
確認する。
test_db=> \d List of relations Schema | Name | Type | Owner --------+------------+-------+------- public | test_table | table | rdera (1 row)
行を入力する。
test_db=> INSERT INTO test_table ("Name", "Number") VALUES ('rdera','000'); INSERT 0 1 test_db=> INSERT INTO test_table ("Name", "Number") VALUES ('sdera','001'); INSERT 0 1
確認する
test_db=> select * from test_table ; Name | Number -------+-------- rdera | 0 sdera | 1 (2 rows)
わーい。
続きます。