Denbun POP版マニュアル
ユーザーマニュアル|スマートフォンMobileガジェットiアプリデンブンP管理者マニュアルインストール方法初期設定ガイド
メニューに戻る
 

■データベース(PostgreSQL)のインストールと設定

PostgreSQLは オープンソースの本格的な RDBMS(Relational Database Management System) です。PostgreSQLに関する詳細は、日本PostgreSQLユーザー会の公式サイト(http://www.postgresql.jp/)をご参照ください。

DenbunPOP版をお使いいただけるPostgreSQLのバージョンについては、『動作環境』をご参照ください。
1.パッケージソースの展開
suコマンドを実行し、rootユーザーにスイッチしてください。

[guest@xxxxxx guest]# su
Password:

FTP転送したファイルからパッケージソースを展開します。この例ではFTP転送したファイルが/home/guestにあるものとします。

[root@xxxxxx guest]# cd /usr/local/src
[root@xxxxxx src]# tar xvzf /home/guest/postgresql-13.3.tar.gz

展開が終了すると、/usr/local/src下にpostgresql-13.3というディレクトリが作成されます。
2.パッケージのインストール
パッケージの構成(Configuration)を行なうために、configureスクリプトを実行します。
ここでは、特にコマンドラインオプションを使用せずにデフォルトで構成します。
configureスクリプトは、ソースを展開したディレクトリに移動して実行します。

[root@xxxxxx src]# cd /usr/local/src/postgresql-13.3/
[root@xxxxxx postgresql-13.3]# ./configure


次にパッケージの構築(Make)を行ないます。パッケージの構築には GNU make を使用します。構築作業には、ハードウェアに依存しますが5分から30分ほど掛かります。

[root@xxxxxx postgresql-13.3]# gmake
      ・
      ・(中略)
      ・
gmake[1]: `all' に対して行うべき事はありません.
gmake[1]: ディレクトリ `/usr/local/src/postgresql-13.3/config' から出ます
All of PostgreSQL successfully made. Ready to install.
[root@xxxxxx postgresql-13.3]#

パッケージの構築が完了したら、インストールを行なう前にPostgreSQLが想定通りに動作することを検証するためのリグレッションテストを行なうことができます。
リグレッションテストは、特権ユーザー(root)では動作しませんので、任意の非特権ユーザーで実行します。ここでは「neo」というユーザーで実行しています。
[root@xxxxxx postgresql-13.3]# su neo
[neo@xxxxxx postgresql-13.3]# gmake check
      ・
      ・(中略)
      ・
=======================
 All 131 tests passed.
=======================

gmake[1]: ディレクトリ `/usr/local/src/postgresql-13.3/src/test/regress' から出ます
[neo@xxxxxx postgresql-13.3]#


最後にパッケージをインストールします。この作業は特権ユーザー(root)で行ないます。
[neo@xxxxxx postgresql-13.3]$ su
パスワード:
[root@xxxxxx postgresql-13.3]# gmake install
      ・
      ・(中略)
      ・
/bin/sh ../config/install-sh -c -m 755 ./install-sh '/usr/local/pgsql/lib/pgxs/config/install-sh'
gmake[1]: ディレクトリ `/usr/local/src/postgresql-13.3/config' から出ます
PostgreSQL installation complete.
[root@xxxxxx postgresql-13.3]#

インストールが完了したら、gmake cleanコマンドでクリーニングを行ないます。これにより、ソースツリーから構築用のファイルを削除しディスク領域を空けることができます。
[root@xxxxxx postgresql-13.3]# gmake clean
3.管理ユーザーの作成
PostgreSQLのインストールが完了したら、データベースの管理ユーザーを作成します。

 

[root@xxxxxx postgresql-13.3]# useradd postgres

必要に応じてpasswdコマンドによってユーザーのパスワードを設定してください。

管理ユーザーは今後PostgreSQLの管理コマンドを利用しますので、作成したpostgresユーザーの環境変数にPATHを設定します。
通常はユーザーのホームディレクトリ下の「.bash_profile」ファイルをviなどで編集して次の記述を追加します。
[root@xxxxxx postgresql-13.3]# cd /home/postgres
[root@xxxxxx postgres]# vi .bash_profile

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=/usr/local/pgsql/bin:$PATH:$HOME/bin ←追加

export PATH
4.データベースの作成
PostgreSQLのデータベースを作成します。
まず、データベースファイルを配置するディレクトリを作成し、ディレクトリの所有者を管理ユーザー(postgres)に変更します。
この例では、データベースファイルの格納先を /var/pgsql/data としています。

 

[root@xxxxxx ~]# mkdir /var/pgsql
[root@xxxxxx ~]# mkdir /var/pgsql/data
[root@xxxxxx ~]# chown -R postgres:postgres /var/pgsql


次に管理ユーザーにて、データベースを作成します。
-Dオプションでデータベースファイルの格納先を指定します。
[root@xxxxxx ~]# su - postgres
[postgres@xxxxxx ~]$ /usr/local/pgsql/bin/initdb \
> --encoding=utf8 --no-locale -D /var/pgsql/data
      ・
      ・(中略)
      ・
Success. You can now start the database server using:

      /usr/local/pgsql/bin/postgres -D /var/pgsql/data
or
      /usr/local/pgsql/bin/pg_ctl -D /var/pgsql/data -l logfile start

[postgres@xxxxxx ~]$


これで、PostgreSQLを起動する準備が整いましたので、PostgreSQLの管理コマンドを利用してPostgreSQLを起動します。
正常に起動できたら、テスト用のデータベースを作成して接続してみます。
[postgres@xxxxxx ~]$ /usr/local/pgsql/bin/pg_ctl -D /var/pgsql/data -l logfile start
server starting
[postgres@xxxxxx ~]$ /usr/local/pgsql/bin/createdb test
[postgres@xxxxxx ~]$ /usr/local/pgsql/bin/psql test
psql (13.3)
Type "help" for help.

test=#

このように表示されれば、データベースは正常に稼動しています。
5.データベースの自動起動設定
PostgreSQLが正常に稼動したら、サーバーの電源投入時にデータベースが自動的に起動するように設定をします。/etc/rc.d/rc.localに記述することもできますが、今回はRed Hat Enterprise Linux を前提としていますので、PostgreSQLに同梱されている自動起動スクリプトを利用して、chkconfigコマンドによる設定を行ないます。
作業は特権ユーザー(root)で行ないます。

PostgreSQLで配布されるソースの中にあるcontrib/start-scripts/linuxファイルを/etc/rc.d/init.d下にpostgresqlというファイル名でコピーし、実行権限を与えます。

 

[postgres@xxxxxx ~]$ su
パスワード:
[root@xxxxxx postgres]# cd /usr/local/src/postgresql-13.3/contrib/start-scripts/
[root@xxxxxx start-scripts]# cp linux /etc/rc.d/init.d/postgresql
[root@xxxxxx start-scripts]# chmod 755 /etc/rc.d/init.d/postgresql


postgresqlファイル内に、パスの記述を設定する所があります。
この中の、データベースファイルの格納先(PGDATA)の記述を環境に合わせて変更します。
[root@xxxxxx ~]# vi /etc/rc.d/init.d/postgresql
      ・
      ・(中略)
      ・
## EDIT FROM HERE

# Installation prefix
prefix=/usr/local/pgsql

# Data directroy
PGDATA="/var/pgsql/data" ←環境に合わせて変更

# Who to run the postmaster as, usually "postgres". (NOT "root")
PGUSER=postgres

# Where to keep a log file
PGLOG="$PGDATA/serverlog"

## STOP EDITING HERE


スクリプトの準備ができたら、chkconfigコマンドによる設定を行ないます。
[root@xxxxxx ~]# chkconfig --add postgresql
[root@xxxxxx ~]# chkconfig --list | grep postgresql
postgresql 0:off 1:off 2:on 3:on 4:on 5:on 6:off
サーバーの電源投入時にデータベースが自動的に起動するように設定されました。
サーバーを再起動して、データベースが自動起動されていることを確認してください。

 
Copyright (C) NEOJAPAN Inc. All Rights Reserved.