実験のためUbuntuにPostgreSQLをインストール

※検索で来られた方には申し訳ないですが、この記事は大学の実験で用いるためのものなのであまり参考にはなりません。


データベースプログラミングの実験のためインストール。実験環境はLinux+Tomcat+PostgreSQL+Java*1

基本的に実験用Webサイト見ながらインストールですが、debian用に書かれているのでUbuntuだと少し変わってきます。実験用WebサイトのURLは貼るとマズイだろうので教科書を参照*2

インストール方法はapt-getとかsynapticパッケージマネージャでもよいのですが、おそらくインストールされる場所などの環境が実験の見本と違ってくるので、知識がないと相当苦労すると思います。

1. postgresグループ、postgresユーザを作成してパスワードを設定する。sudoで行う。Ubuntuだとデフォルトでこのユーザが登録されているらしいが*3、もし登録されていたら「既に登録されている」と出るので問題なし。ただし後にあるホームディレクトリ等が変わってくる。

yuyarin@ubuntu$ sudo /usr/sbin/groupadd postgres
yuyarin@ubuntu$ sudo /usr/sbin/useradd -g postgres postgres
yuyarin@ubuntu$ sudo passwd postgres

2. PostgreSQLをインストールするためのディレクトリを作成して所有者をpostgresに変更する。sudoで行う。

yuyarin@ubuntu$ sudo mkdir /usr/local/src/pgsql
yuyarin@ubuntu$ sudo chown postgres:postgres /usr/local/src/pgsql
yuyarin@ubuntu$ sudo mkdir /usr/local/pgsql
yuyarin@ubuntu$ sudo chown postgres:postgres /usr/local/pgsql

3. インストールの前に環境を最新にして、必要なパッケージをインストールする。

yuyarin@ubuntu$ sudo apt-get update
yuyarin@ubuntu$ sudo apt-get install zlib1g-dev libreadline5-dev

4. ユーザをpostgresに変更する。

yuyarin@ubuntu$ su postgres

もしユーザ変更した後に

postgres@ubuntu$

ではなく

$

と表示されていて、矢印キーを押すと"[ [A"などと表示される場合は、シェルがbashではなくshになっているので/bin/bashを打つ。また、手順7のpasswdファイルに/bin/bashを記述する。


5. PostgreSQL用のディレクトリに移動してソースコードを実験用Webサイトからダウンロードし、展開する。

postgres@ubuntu$ cd /usr/local/src/pgsql
postgres@ubuntu$ wget http://***********.ac.jp/******/postgresql-8.2.5.tar.gz
...
postgres@ubuntu$ tar zxvf postgresql-8.2.5.tar.gz

6. コンパイルしてインストールする。

postgres@ubuntu$ cd postgresql-8.2.5
postgres@ubuntu$ ./configure
postgres@ubuntu$ make all
postgres@ubuntu$ make install

7. もしpostgresのホームディレクトリが/home/postgresでなければ、ディレクトリを作ってpasswdを編集して設定する(このへんうろ覚えなので……)。あと所有権の変更等は必要ならば。

postgres@ubuntu$ sudo mkdir /home/postgres
postgres@ubuntu$ sudo gedit /etc/passwd
postgres@ubuntu$ sudo chown postgres:postgres /home/postgre

こんなかんじ。

postgres:x:110:120:PostgreSQL administrator,,,:/home/postgres:/bin/bash

8. bashのプロファイルを作成

postgres@ubuntu$ sudo gedit /home/postgres/.bash_profile

中身。

PGDATA=/usr/local/pgsql/data
[ -f $PGDATA/../initdb.i18n ] && source $PGDATA/../initdb.i18n
export PATH=$PATH:/usr/local/pgsql/bin
export POSTGRES_HOME=/usr/local/pgsql
export PGLIB=$POSTGRES_HOME/lib
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH":"$PGLIB"
export MANPATH="$MANPATH":$POSTGRES_HOME/man
export PGDATA=$POSTGRES_HOME/data

9. 編集した.bash_profileを実行する。

postgres@ubuntu$ source /home/postgres/.bash_profile

10. データベースを初期化する。

postgres@ubuntu$ initdb -D /usr/local/pgsql/data/

linuxマスターの方々の指摘待ち。

*1:インストールできない人、というかそもそも学科PCのDebianでネットに繋がらない人が続出したのでVMware用のUbuntuのDVDが用意してあるとか。しかもそいつは既に実験環境インストール済み。

*2:2006を2007に訂正する必要有り。

*3:というよりデフォルトでPostgreSQLが入っているらしいが、確認はしていない。