(最終更新日時:2009-02-21 08:24:13)
オープンソースのリレーショナルデータベース管理システム(RDBMS)
PostgreSQL をインストールします。
ダウンロード
| ・ | PostgreSQL | : | postgresql-8.3.5.tar.gz |
インストール
PostgreSQLの実行用グループとユーザーを作成します。
# groupadd postgres
# useradd -g postgres -d /usr/local/pgsql postgres
/usr/local/srcディレクトリでアーカイブを展開し、コンパイルおよびインストールを行います。
# cd /usr/local/src
# tar zxvf ダウンロード先/postgresql-8.3.5.tar.gz
# chown -R postgres.postgres /usr/local/src/postgresql-8.3.5
# su - postgres コンパイルはpostgresユーザーで行います
$ cd /usr/local/src/postgresql-8.3.5
$ ./configure \
> --enable-nls=ja \ 各国語サポートを有効にします
> --with-openssl SSL接続を有効にします
$ make
$ make check リグレッションテストを行います
$ make install-strip
$ exit
設定
データベースの格納先を作成します。
ここでは、
/home/database/pgsql を格納先にしています。
# mkdir -p /home/database/pgsql
# chown -R postgres.postgres /home/database/pgsql
環境変数を追加します。
# vi /etc/profile
:
export PG_HOME=/usr/local/pgsql
# source /etc/profile 追加した環境変数を適用します
データベースを初期化します。
# su - postgres postgresユーザーで行います
$ initdb -D /home/database/pgsql -E UTF8 --no-locale
$ exit
ネットワーク上の他のマシンから直接PostgreSQLサーバにアクセスする場合は、下記の設定を行います。
(例は
192.168.10.0/24 からアクセスする場合)
# vi /home/database/pgsql/postgresql.conf
:
# - Connection Settings -
#listen_addresses = 'localhost' # what IP interface(s) to listen on;
# defaults to localhost, '*' = any
listen_addresses = 'localhost,192.168.10.0/24'
:
自動起動設定
自動起動用のスクリプトファイルをコピーし、編集します。
# cp /usr/local/src/postgresql-8.3.0/contrib/start-scripts/linux /etc/rc.d/init.d/pgsql
# vi /etc/rc.d/init.d/pgsql
:
## EDIT FROM HERE
# Source function library.
. /etc/init.d/functions
:
PGDATA="/home/database/pgsql"
:
start)
echo -n "Starting PostgreSQL: "
su - $PGUSER -c "$DAEMON -D '$PGDATA' &" >>$PGLOG 2>&1
echo_success
echo
;;
stop)
echo -n "Stopping PostgreSQL: "
su - $PGUSER -c "$PGCTL stop -D '$PGDATA' -s -m fast" >/dev/null
echo_success
echo
;;
restart)
echo -n "Restarting PostgreSQL: "
su - $PGUSER -c "$PGCTL stop -D '$PGDATA' -s -m fast -w" >/dev/null
su - $PGUSER -c "$DAEMON -D '$PGDATA' &" >>$PGLOG 2>&1
echo_success
echo
;;
reload)
echo -n "Reload PostgreSQL: "
su - $PGUSER -c "$PGCTL reload -D '$PGDATA' -s"
echo_success
echo
;;
:
# chmod 755 /etc/rc.d/init.d/pgsql
# chkconfig pgsql on
# service pgsql start
パスワード接続設定
セキュリティを考慮し、データベースへの接続はパスワードを必須にします。
スーパーユーザ
postgres にパスワードを設定します。
# su - postgres postgresユーザーで行います
$ psql
postgres=# ALTER USER postgres WITH PASSWORD 'パスワード';
postgres=# \q
$ exit
pg_hba.conf を編集し、パスワード接続を必須にします。
# vi /home/database/pgsql/pg_hba.conf
:
# "local" is for Unix domain socket connections only
#local all all trust
local all postgres md5
# IPv4 local connections:
#host all all 127.0.0.1/32 trust
host all postgres 127.0.0.1/32 md5
# IPv6 local connections:
#host all all ::1/128 trust
host all postgres ::1/128 md5
JDBCドライバ
JDBCドライバは、
| ・ | JDBC4 Postgresql Driver, Version 8.3-603 | : | postgresql-8.3-603.jdbc4.jar |
運用
ユーザデータベースを作成します。
# su - postgres postgresユーザーで行います
$ createdb データベース名
Password: postgresの接続パスワード
$ exit
ユーザデータベースを操作します。
# su - postgres postgresユーザーで行います
$ psql データベース名
Password: postgresの接続パスワード
:
データベース名=# 操作コマンド;
:
データベース名=# \q
$ exit
バックアップを行います。
# su - postgres postgresユーザーで行います
$ pg_dumpall -c > バックアップファイル名
Password: postgresの接続パスワード
$ exit
リストアを行います。
# su - postgres postgresユーザーで行います
$ psql -f バックアップファイル名
Password: postgresの接続パスワード
$ exit
メジャーバージョンアップを行います。
# (# バージョンアップ版のコンパイルのみ行います)
# cd /usr/local/src
# tar zxvf ダウンロード先/postgresql-8.3.5.tar.gz
# chown -R postgres.postgres /usr/local/src/postgresql-8.3.5
# su - postgres コンパイルはpostgresユーザーで行います
$ cd /usr/local/src/postgresql-8.3.5
$ ./configure \
> --enable-nls=ja \ 各国語サポートを有効にします
> --with-openssl SSL接続を有効にします
$ make
$ make check リグレッションテストを行います
$ exit
# (# バックアップを取得します)
# su - postgres postgresユーザーで行います
$ pg_dumpall > バックアップファイル名
Password: postgresの接続パスワード
# exit
# (# サービスを停止します)
# service pgsql stop
# (# 念のためデータベースフォルダごとバックアップします)
# mv /home/database/pgsql /home/database/pgsql_bak
# (# バージョンアップ版のインストールを行います)
# su - postgres postgresユーザーで行います
$ cd /usr/local/src/postgresql-8.3.5
$ make install-strip
$ exit
# (# データベースを初期化します)
# mkdir -p /home/database/pgsql
# chown -R postgres.postgres /home/database/pgsql
# su - postgres postgresユーザーで行います
$ initdb -D /home/database/pgsql -E UTF8 --no-locale
$ exit
# (# サービスを開始します)
# service pgsql start
# (# リストアを行います)
# su - postgres postgresユーザーで行います
$ psql -f バックアップファイル名
$ exit
# (# pg_hba.confの編集を行います)
# vi /home/database/pgsql/pg_hba.conf
:
# (# サービスを再起動します)
# service pgsql restart
Copyright © 2004-2010 System House ACT. All Rights Reserved.