endless pulse

どっくんどっくんふるえる毎日を過ごしています

postgreSQL(9.1)を使ってみた for Mac OS X 10.6

修論やろうね?←
ごめんなさいごめんなさい今週末のTOEIC終わったら本気出しますほんとにごめんなさいごめんなさいごめんなさい。


ひょんなことからPHPからpostgreSQLなるもの(ぽすぐれ、と呼ばれているらしい)を使う流れになったのでその備忘録を。何が苦労したって、環境構築だよねw

postgreSQLのインストール with MacPorts

Macportsでsearchしたらいっぱいでたので、ライトユーザーな私はMacportsを例に漏れず使用しました。
11/13の時点でpostgreSQLの最新バージョンは9.1、PHPは5.3.6かな。
そんなわけで最新の9.1のpostgreSQLをぽーと☆いんすとーる!(疲れているようなのでほうっておいて上げて下さい)

とりあえずpostgreSQLインストール
$ sudo port install postgresql91

--->  Computing dependencies for postgresql91
--->  Dependencies to be installed: bison libxslt ossp-uuid
--->  Fetching archive for bison
--->  Attempting to fetch bison-2.5_0.darwin_10.x86_64.tgz from http://packages.macports.org/bison
--->  Fetching bison
--->  Attempting to fetch bison-2.5.tar.bz2 from ftp://ftp.dti.ad.jp/pub/GNU/bison
--->  Attempting to fetch bison-2.5.tar.bz2 from http://mirror.internode.on.net/pub/gnu/bison
--->  Attempting to fetch bison-2.5.tar.bz2 from http://distfiles.macports.org/bison
--->  Verifying checksum(s) for bison
--->  Extracting bison
--->  Configuring bison
--->  Building bison
--->  Staging bison into destroot
--->  Installing bison @2.5_0
--->  Activating bison @2.5_0
--->  Cleaning bison
--->  Fetching archive for libxslt
--->  Attempting to fetch libxslt-1.1.26_0.darwin_10.x86_64.tgz from http://packages.macports.org/libxslt
--->  Fetching libxslt
--->  Attempting to fetch libxslt-1.1.26.tar.gz from http://distfiles.macports.org/libxslt
--->  Verifying checksum(s) for libxslt
--->  Extracting libxslt
--->  Configuring libxslt
--->  Building libxslt
--->  Staging libxslt into destroot
--->  Installing libxslt @1.1.26_0
--->  Activating libxslt @1.1.26_0
--->  Cleaning libxslt
--->  Fetching archive for ossp-uuid
--->  Attempting to fetch ossp-uuid-1.6.2_0.darwin_10.x86_64.tgz from http://packages.macports.org/ossp-uuid
--->  Fetching ossp-uuid
--->  Attempting to fetch uuid-1.6.2.tar.gz from ftp://ftp.jp.FreeBSD.org/pub/FreeBSD/ports/distfiles/
--->  Verifying checksum(s) for ossp-uuid
--->  Extracting ossp-uuid
--->  Applying patches to ossp-uuid
--->  Configuring ossp-uuid
--->  Building ossp-uuid
--->  Staging ossp-uuid into destroot
--->  Installing ossp-uuid @1.6.2_0
--->  Activating ossp-uuid @1.6.2_0
--->  Cleaning ossp-uuid
--->  Fetching archive for postgresql91
--->  Attempting to fetch postgresql91-9.1.1_0.darwin_10.x86_64.tgz from http://packages.macports.org/postgresql91
--->  Fetching postgresql91
--->  Attempting to fetch postgresql-9.1.1.tar.bz2 from http://ftp9.us.postgresql.org/pub/mirrors/postgresql/source/v9.1.1/
--->  Verifying checksum(s) for postgresql91
--->  Extracting postgresql91
--->  Configuring postgresql91
--->  Building postgresql91
--->  Staging postgresql91 into destroot
--->  Installing postgresql91 @9.1.1_0

To use the postgresql server, install the postgresql91-server port
--->  Activating postgresql91 @9.1.1_0
--->  Cleaning postgresql91


serverをいれろ、という表示に見えたのでそっちも。

$ sudo port install postgresql91-server

--->  Computing dependencies for postgresql91-server
--->  Fetching archive for postgresql91-server
--->  Attempting to fetch postgresql91-server-9.1.1_0.darwin_10.x86_64.tgz from http://packages.macports.org/postgresql91-server
--->  Fetching postgresql91-server
--->  Verifying checksum(s) for postgresql91-server
--->  Extracting postgresql91-server
--->  Configuring postgresql91-server
--->  Building postgresql91-server
--->  Staging postgresql91-server into destroot
--->  Creating launchd control script
###########################################################
# A startup item has been generated that will aid in
# starting postgresql91-server with launchd. It is disabled
# by default. Execute the following command to start it,
# and to cause it to launch at startup:
#
# sudo port load postgresql91-server
###########################################################
--->  Installing postgresql91-server @9.1.1_0

To create a database instance, after install do
 sudo mkdir -p /opt/local/var/db/postgresql91/defaultdb
 sudo chown postgres:postgres /opt/local/var/db/postgresql91/defaultdb
 sudo su postgres -c '/opt/local/lib/postgresql91/bin/initdb -D /opt/local/var/db/postgresql91/defaultdb'

To tweak your DBMS, consider increasing kern.sysv.shmmax by adding an increased kern.sysv.shmmax .. to /etc/sysctl.conf
--->  Activating postgresql91-server @9.1.1_0
--->  Cleaning postgresql91-server

すごく大事そうなことが出力されてますねー。あとでやりましょう。

ちなみにselectもいれてみました。これは別バージョンに対応させないならいれなくてもいいかも。

$ sudo port install postgresql_select 

--->  Fetching archive for postgresql_select
--->  Attempting to fetch postgresql_select-0.1_0.darwin_10.noarch.tgz from http://packages.macports.org/postgresql_select
--->  Fetching postgresql_select
--->  Verifying checksum(s) for postgresql_select
--->  Extracting postgresql_select
--->  Configuring postgresql_select
--->  Building postgresql_select
--->  Staging postgresql_select into destroot
--->  Installing postgresql_select @0.1_0
--->  Activating postgresql_select @0.1_0
--->  Cleaning postgresql_select

他2つは結構時間かかったけど、これはすぐ。

PHP用のドライバ

PHP用のドライバ?みたいなものが必要ぽいことがネット徘徊してたら書いてあったので、探してみた。

php5-postgresqlの説明は以下

php5-postgresql @5.3.8 (php, databases)
    a PHP interface to PostgreSQL, including the pgsql and pdo_pgsql extensions

てなわけでいれる。

$ sudo port install php5-postgresql

Warning: port definitions are more than two weeks old, consider using selfupdate
--->  Computing dependencies for php5-postgresql
--->  Dependencies to be installed: php5 apache2 apr-util autoconf213 gsed mhash postgresql90
--->  Fetching archive for apr-util
--->  Attempting to fetch apr-util-1.3.12_0.darwin_10.x86_64.tgz from http://packages.macports.org/apr-util
--->  Fetching apr-util
--->  Attempting to fetch apr-util-1.3.12.tar.bz2 from ftp://ftp.infoscience.co.jp/pub/net/apache/dist/apr
--->  Attempting to fetch apr-util-1.3.12.tar.bz2 from http://mirror.internode.on.net/pub/apache/apr
--->  Verifying checksum(s) for apr-util
--->  Extracting apr-util
--->  Configuring apr-util
--->  Building apr-util
--->  Staging apr-util into destroot
--->  Installing apr-util @1.3.12_0
--->  Activating apr-util @1.3.12_0
--->  Cleaning apr-util
--->  Fetching archive for apache2
--->  Attempting to fetch apache2-2.2.21_2+preforkmpm.darwin_10.x86_64.tgz from http://packages.macports.org/apache2
--->  Fetching apache2
--->  Attempting to fetch httpd-2.2.21.tar.bz2 from ftp://ftp.infoscience.co.jp/pub/net/apache/dist/httpd
--->  Attempting to fetch httpd-2.2.21.tar.bz2 from http://mirror.internode.on.net/pub/apache/httpd
--->  Verifying checksum(s) for apache2
--->  Extracting apache2
--->  Applying patches to apache2
--->  Configuring apache2
--->  Building apache2
--->  Staging apache2 into destroot
--->  Creating launchd control script
###########################################################
# A startup item has been generated that will aid in
# starting apache2 with launchd. It is disabled
# by default. Execute the following command to start it,
# and to cause it to launch at startup:
#
# sudo port load apache2
###########################################################
Warning: apache2 installs files outside the common directory structure.
--->  Installing apache2 @2.2.21_2+preforkmpm
--->  Activating apache2 @2.2.21_2+preforkmpm
--->  Cleaning apache2
--->  Fetching archive for autoconf213
--->  Attempting to fetch autoconf213-2.13_1.darwin_10.noarch.tgz from http://packages.macports.org/autoconf213
--->  Fetching autoconf213
--->  Attempting to fetch autoconf-2.13.tar.gz from ftp://ftp.dti.ad.jp/pub/GNU/autoconf
--->  Verifying checksum(s) for autoconf213
--->  Extracting autoconf213
--->  Configuring autoconf213
--->  Building autoconf213
--->  Staging autoconf213 into destroot
--->  Installing autoconf213 @2.13_1
--->  Activating autoconf213 @2.13_1
--->  Cleaning autoconf213
--->  Fetching archive for gsed
--->  Attempting to fetch gsed-4.2.1_2.darwin_10.x86_64.tgz from http://packages.macports.org/gsed
--->  Fetching gsed
--->  Attempting to fetch sed-4.2.1.tar.gz from ftp://ftp.dti.ad.jp/pub/GNU/sed
--->  Verifying checksum(s) for gsed
--->  Extracting gsed
--->  Configuring gsed
--->  Building gsed
--->  Staging gsed into destroot
--->  Installing gsed @4.2.1_2
--->  Activating gsed @4.2.1_2
--->  Cleaning gsed
--->  Fetching archive for mhash
--->  Attempting to fetch mhash-0.9.9.9_0.darwin_10.x86_64.tgz from http://packages.macports.org/mhash
--->  Fetching mhash
--->  Attempting to fetch mhash-0.9.9.9.tar.bz2 from http://jaist.dl.sourceforge.net/project/mhash/mhash/0.9.9.9
--->  Verifying checksum(s) for mhash
--->  Extracting mhash
--->  Configuring mhash
--->  Building mhash
--->  Staging mhash into destroot
--->  Installing mhash @0.9.9.9_0
--->  Activating mhash @0.9.9.9_0
--->  Cleaning mhash
--->  Fetching archive for php5
--->  Attempting to fetch php5-5.3.8_1+apache2.darwin_10.x86_64.tgz from http://packages.macports.org/php5
--->  Fetching php5
--->  Attempting to fetch php-5.3.8.tar.bz2 from http://jp2.php.net/distributions/
--->  Verifying checksum(s) for php5
--->  Extracting php5
--->  Applying patches to php5
--->  Configuring php5
--->  Building php5
--->  Staging php5 into destroot
Warning: php5 installs files outside the common directory structure.
--->  Installing php5 @5.3.8_1+apache2
--->  Activating php5 @5.3.8_1+apache2
To customize php, copy
/opt/local/etc/php5/php.ini-development (if this is a development server) or
/opt/local/etc/php5/php.ini-production (if this is a production server) to
/opt/local/etc/php5/php.ini and then make changes.

If this is your first install, you need to activate PHP in your web server.

To enable PHP in Apache, run
  cd /opt/local/apache2/modules
  /opt/local/apache2/bin/apxs -a -e -n "php5" libphp5.so
--->  Cleaning php5
--->  Fetching archive for postgresql90
--->  Attempting to fetch postgresql90-9.0.5_0.darwin_10.x86_64.tgz from http://packages.macports.org/postgresql90
--->  Fetching postgresql90
--->  Attempting to fetch postgresql-9.0.5.tar.bz2 from http://ftp9.us.postgresql.org/pub/mirrors/postgresql/source/v9.0.5/
--->  Verifying checksum(s) for postgresql90
--->  Extracting postgresql90
--->  Configuring postgresql90
--->  Building postgresql90
--->  Staging postgresql90 into destroot
--->  Installing postgresql90 @9.0.5_0

To use the postgresql server, install the postgresql90-server port
--->  Activating postgresql90 @9.0.5_0
--->  Cleaning postgresql90
--->  Fetching archive for php5-postgresql
--->  Attempting to fetch php5-postgresql-5.3.8_0+postgresql90.darwin_10.x86_64.tgz from http://packages.macports.org/php5-postgresql
--->  Fetching php5-postgresql
--->  Verifying checksum(s) for php5-postgresql
--->  Extracting php5-postgresql
--->  Configuring php5-postgresql
--->  Building php5-postgresql
--->  Staging php5-postgresql into destroot
--->  Installing php5-postgresql @5.3.8_0+postgresql90
--->  Activating php5-postgresql @5.3.8_0+postgresql90
--->  Cleaning php5-postgresql

ながい。

あれ、ていうかこのときにpostgresqlver9.0が入ってたのか。うーむ。…まあいいかな。
噂によると、postgresqlをportでインストールした最初に+php オプションで入ったみたい。

To create a database instance

それが完了してから、サーバーインストール時に書いてあったあたりを参考に…したはずなのに間違えました\(^o^)/
たぶん下記を実行すればいいはずですが。

$sudo mkdir -p /opt/local/var/db/postgresql91/defaultdb
$sudo chown postgres:postgres /opt/local/var/db/postgresql91/defaultdb
$sudo su postgres -c '/opt/local/lib/postgresql91/bin/initdb -D /opt/local/var/db/postgresql91/defaultdb'

もしこのエントリ見てpostgreSQLのインストールする方は上記をやってね!そしてもしこのエントリ見て次にまた自分がやるときも上記をやろうね!←


とりあえずmkdirの場所間違えて以下に作りました。

$sudo mkdir /opt/local/lib/postgresql91/data

たぶんlocate postgresqlとかやってそこに作ったんだろうなーメモに当然のようにここに作ったと書かれていて、よく覚えてないけど。。
また、postgreSQLpostgreSQLを触るユーザを指定しなければいけないらしい(rootじゃまずい)のだけど、それを我がユーザ名「glorypulse」に設定してし ま っ た w(上のデフォルトのやつであれば、ユーザ名はpostgres)そんなわけで以下のコマンドを打ちました。

$sudo chown glorypulse:postgres /opt/local/lib/postgresql91/data

というわけでその次ももちろん変わって、

$sudo su glorypulse -c '/opt/local/lib/postgresql91/bin/initdb -D /opt/local/lib/postgresql91/data'
The files belonging to this database system will be owned by user "glorypulse".
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/lib/postgresql91/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 20
selecting default shared_buffers ... 1600kB
creating configuration files ... ok
creating template1 database in /opt/local/lib/postgresql91/data/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

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

Success. You can now start the database server using:

    /opt/local/lib/postgresql91/bin/postgres -D/opt/local/lib/postgresql91/data
or
    /opt/local/lib/postgresql91/bin/pg_ctl -D /opt/local/lib/postgresql91/data -l logfile start
起動します。
$ sudo su postgres -c '/opt/local/lib/postgresql91/bin/postgres -D /opt/local/var/db/postgresql91/defaultdb &'

とやればいいと思います。
でもそのあたりをまとめたのがpg_ctlというコマンドらしいので、それを利用するのも手のようですね!(上記initdbの初期化でそう出てました。)

sudo su postgres -c '/opt/local/lib/postgresql91/bin/pg_ctl -D /opt/local/var/db/postgresql91/defaultdb -l logfile start'
試しに使ってみる
psql -D postgres -u postgres

でいいとおもいます。自分の場合はユーザ名がpostgresじゃなかったのでこのコマンド自体を試したわけではないけれど。


ちなみに、結局データベースを触るときにはpgAdmin3とかいうGUIソフトが便利だったので使ってました。DBの起動までをやって、こいつを起動すれば、楽しく使えると思います。かなり直観的。


後日談。

環境変数

initdbとかpostgreとかpg_ctlが /opt/local/lib/postgresql91/bin 内にあるので、私はそこにPATHを通したのと、環境変数PGDATAがinitdbなどの-D指定のデフォルトになるので、いちいち指定がめんどうな場合は.bashrcなどに記載しておくといいと思います。

PATH="$PATH":/opt/local/lib/postgresql91/bin
export POSTGRES_HOME=/opt/local/lib/postgresql91
export PGLIB=$POSTGRES_HOME/lib
export PGDATA=$POSTGRES_HOME/data
export MANPATH="$MANPTH":$POSTGRES_HOME/man
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH":"$PGLIB"

(参考:Postgresスタートについて - アキヒーロの開発日記

起動できない…

ええと、その次の日にもう一回起動しようとしたら全然できませんでした\(^o^)/

$pg_ctl start
server starting

とかやっても、でもpgAdmin3ではport5432が開かれてないんじゃないの?みたいなエラーが出るし、もちろんコマンドも応答しない。statusは…?と思い、打ち込むも、

$pg_ctl status
pg_ctl: no server running

という悲しい結果に。

ちょっと途中何やったか忘れてしまったのだけど、postgresql.confとかpg_hba.confあたりを設定したりして(sampleが私の場合は /opt/local/share/postgresql91/postgresql.conf.sample にありました。)起動してみたら、

FATAL: "/opt/local/lib/postgresql91/data" is not a valid data directory
DETAIL: File /opt/local/lib/postgresql91/data/PG_VERSION is missing.

とかいうエラーに変わった。
PG_VERSION自体はversionが書いてあるだけのファイルっぽかったので、「9.1」とか書いて作ったとしてもまた他のファイルがないって言われる。


ら、実は最初に作ったときにここ(環境変数PGDATAにも登録した、/opt/local/lib/postgresql91/data)に作ってなかったwwwww
というわけで上のエントリは嘘付いてるみたいですね!←


もしちゃんとinitdbしてあれば、以下のようなファイル構成になると思います。

(initdbしたフォルダをlsした図)


これで半日ハマってたとか恥ずかしくて言えない。

PHPで使ってみよう!

少し古いけれど、以下のサイトがよかった。

あとはPHPの公式マニュアルのpg_なんとか、っていうメソッド見てとりあえずぱちぱち作ってました。

結局つかったの?

herokuという初期投資無料のクラウドサーバーサービスで、postgreSQLが主流〜みたいな話を聞き、自分で調べもせず作り出してしまったので、いざサーバーにあげようかな!と思って見て見たら、2011/11現在では月2万円からの有料プランしかないぞ!!!

そんなわけでただ遊んでみただけになってしまいました。
すいませんでした。修論やります。