ChatLuckのインストールとデータベース構築

データベースサーバー(PostgreSQL 11)のインストールと設定についての説明です。

ここではお使いのサーバーに、『PostgreSQL 11』をインストールする手順を説明します。

インストール前にご一読ください

PostgreSQLは、本格的なオープンソースRDBMS(Relational Database Management System)です。

PostgreSQLに関する詳細は、「日本PostgreSQLユーザー会」の公式サイトをご参照ください。

ご注意

ChatLuckでご利用いただけるPostgreSQLのバージョンは、「動作環境」をご確認の上、インストールを行ってください。
既にPostgreSQLが導入され、正常に稼動している場合でも「postgres_fdw」の追加インストールが必要となります。

これからインストールを行うサーバー上に、既に別バージョンのPostgreSQLがインストールされていると、予期しない結果となり、ChatLuckが正しく稼動しない場合がありますので、インストール前に必ずシステム状態をご確認ください。

1.パッケージソースの展開

suコマンドにて、rootユーザーにスイッチしてください。

[guest@xxxxxx guest]$ su -
Password:
[root@xxxxxx guest]#

 

ダウンロードサイトからダウンロードしたPostgreSQLのパッケージソースを展開します。ここでは、guestユーザーのホームディレクトリにあるものとします。

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

展開が完了すると、/usr/local/srcディレクトリ下に「postgresql-11.10」というディレクトリが作成されます。

2.パッケージのインストール

インストールするサーバー環境による構成を行います。

[root@xxxxxx guest]# cd postgresql-11.10
[root@xxxxxx postgresql-11.10]# ./configure

 ・

 ・(中略)

 ・

configure: using LDFLAGS= -Wl,--as-needed
configure: creating ./config.status
config.status: creating GNUmakefile
config.status: creating src/Makefile.global
config.status: creating src/include/pg_config.h
config.status: creating src/include/pg_config_ext.h
config.status: creating src/interfaces/ecpg/include/ecpg_config.h
config.status: linking src/backend/port/tas/dummy.s to src/backend/port/tas.s
config.status: linking src/backend/port/dynloader/linux.c to src/backend/port/dynloader.c
config.status: linking src/backend/port/sysv_sema.c to src/backend/port/pg_sema.c
config.status: linking src/backend/port/sysv_shmem.c to src/backend/port/pg_shmem.c
config.status: linking src/backend/port/unix_latch.c to src/backend/port/pg_latch.c
config.status: linking src/backend/port/dynloader/linux.h to src/include/dynloader.h
config.status: linking src/include/port/linux.h to src/include/pg_config_os.h
config.status: linking src/makefiles/Makefile.linux to src/Makefile.port

[root@xxxxxx postgresql-11.10]#

※ここでは、特にコマンドオプションを指定せず、デフォルトのインストール先(/usr/local/pgsql)にインストールされる構成で説明します。インストール先等変更したい場合には"--prefix"オプションなど、PostgreSQLのインストールガイドをご参照ください。

サーバー上に不足しているパッケージなどがある場合、ここで構成エラーが発生しますので、不足しているパッケージを追加インストールするなどのご対応をお願いいたします。

 

次に、パッケージのコンパイルを行います。ここではGNU makeコマンドを使用してコンパイルします。

コンパイルは、お使いのハードウェアスペックにより、数分から数十分を要しますので、完了までお待ちください。

[root@xxxxxx postgresql-11.10]# gmake

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

 

コンパイルが完了したら、インストールを行う前に、PostgreSQLが正しく動作するかを検証するためにリグレッションテストを行います。

リグレッションテストは、rootユーザーでは実行することができませんので、rootユーザー以外のユーザーにスイッチします。(ここでは「guest」ユーザーで実行します。)

[root@xxxxxx postgresql-11.10]# su guest

[guest@xxxxxx postgresql-11.10]$ gmake check
 ・
 ・(中略)
 ・
     sequence            ... ok
     polymorphism     ... ok
     rowtypes            ... ok
     returning            ... ok
     largeobject         ... ok
     with                   ... ok
     xml                    ... ok
test event_trigger     ... ok
test stats                 ... ok
============== shutting down postmaster               ==============
============== removing temporary instance          ==============

=======================
 All 167 tests passed.
=======================

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

"All 167 tests passed."のように表示されればテストが正しく完了していますので、インストールを行います。

インストールは、rootユーザーで行います。先ほどスイッチした「guest」ユーザーから戻します。

[guest@xxxxxx postgresql-11.10]$ exit

[root@xxxxxx postgresql-11.10]# gmake install
 ・
 ・(中略)
 ・
gmake[1]: ディレクトリ `/usr/local/src/postgresql-11.10/src' から出ます
gmake -C config install
gmake[1]: ディレクトリ `/usr/local/src/postgresql-11.10/config' に入ります
/bin/mkdir -p '/usr/local/pgsql11.10/lib/pgxs/config'
/usr/bin/install -c -m 755 ./install-sh '/usr/local/pgsql11.10/lib/pgxs/config/install-sh'
/usr/bin/install -c -m 755 ./missing '/usr/local/pgsql11.10/lib/pgxs/config/missing'
gmake[1]: ディレクトリ `/usr/local/src/postgresql-11.10/config' から出ます
PostgreSQL installation complete.
[root@xxxxxx postgresql-11.10]#

"PostgreSQL installation complete."が表示されればインストールは完了です。

 

次に、ログ収集を利用するため、PostgreSQLの"postgres_fdw"機能をコンパイルします。

[root@xxxxxx postgresql-11.10]# cd contrib/postgres_fdw

[root@xxxxxx postgres_fdw]# gmake


・(中略)

gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -O2 -fPIC -I../../src/interfaces/libpq -I. -I. -I../../src/include -D_GNU_SOURCE -c -o shippable.o shippable.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -O2 -fPIC -shared -o postgres_fdw.so postgres_fdw.o option.o deparse.o connection.o shippable.o -L../../src/port -L../../src/common -Wl,--as-needed -Wl,-rpath,'/usr/local/pgsql11.10/lib',--enable-new-dtags -L../../src/interfaces/libpq -lpq

[root@xxxxxx postgres_fdw]#


コンパイルが完了したら、インストールします。

[root@xxxxxx postgres_fdw]# gmake install


・(中略)

/usr/bin/install -c -m 755 postgres_fdw.so '/usr/local/pgsql/lib/postgres_fdw.so'
/usr/bin/install -c -m 644 ./postgres_fdw.control '/usr/local/pgsql/share/extension/'
/usr/bin/install -c -m 644 ./postgres_fdw--1.0.sql '/usr/local/pgsql/share/extension/'

[root@xxxxxx postgres_fdw]#

 

最後に、パッケージのコンパイルで生成されたオブジェクトファイルを削除することで、使用しているディスク容量を空けることができますので、必要に応じて実行してください。

[root@xxxxxx postgres_fdw]# cd ../..

[root@xxxxxx postgresql-11.10]# gmake clean

 

3.データベース管理ユーザーの作成

次に、PostgreSQLデータベースの管理ユーザー"postgres"(以下、"postgres"ユーザー)を作成します。

※必要に応じて、ユーザーパスワードを設定してください。

[root@xxxxxx postgresql-11.10]# useradd postgres

[root@xxxxxx postgresql-11.10]# passwd postgres

Password:

作成したユーザーの環境変数に、PostgreSQLコマンドまでのパスを通します。これにより、作成した"postgres"ユーザーが、PostgreSQLデータベース管理コマンドなどを実行することができるようになります。

viコマンドで「.bash_profile」を開き、PATH変数を編集します。

[root@xxxxxx postgresql-11.10]# cd /home/postgres

[root@xxxxxx postgres]# vi .bash_profile

以下のパスを環境変数「PATH」に追加設定してください。

# .bash_profile

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

# User specific environment and startup programs

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

export PATH

編集が完了したら、viコマンドの":wq"(保存して閉じる)を実行してください。

 

4.データベースクラスターの初期化

PostgreSQLのデータベースクラスターの初期化を行います。

データベース格納先のディレクトリを作成し、"postgres"ユーザーをディレクトリオーナーに設定します。

[root@xxxxxx postgres]# mkdir -p /var/pgsql/data

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

次に、"postgres"ユーザーにスイッチして、データベースクラスターを初期化します。

ここでは、先に作成した"/var/pgsql/data"をデータベースファイル格納先として指定します。

[root@xxxxxx postgres]# su - postgres

[postgres@xxxxxx ~]$ initdb --encoding=utf8 --locale=C -D /var/pgsql/data

・(中略)

Success. You can now start the database server using:

     pg_ctl -D /var/pgsql/data -l logfile start


[postgres@xxxxxx ~]$

ここまで完了すると、PostgreSQLデータベースを起動する準備が整いましたので、PostgreSQLを起動します。

[postgres@xxxxxx ~]$ pg_ctl -D /var/pgsql/data -l logfile start

server starting

[postgres@xxxxxx ~]$

正常に起動できたら、確認用のデータベースを作成し、接続確認しましょう。

[postgres@xxxxxx ~]$ createdb test

[postgres@xxxxxx ~]$ psql test

psql (11.10)

Type "help" for help.

 

test=#

正常に接続できたら、データベース一覧(\lコマンド)を実行し、正しく作成できていることをご確認ください。

※作成した「test」データベースのCollate、Ctypeが"C"となっていない場合、ChatLuckが正しく動作しない可能性がありますので、ご注意ください。(クラスターの初期化に誤りがある可能性があります。)

test=# \l


                             List of databases
   Name    |  Owner   | Encoding | Collate | Ctype |   Access privileges
-----------+----------+----------+---------+-------+-----------------------
 postgres  | postgres | UTF8     | C       | C     |
 template0 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
 template1 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
 test      | postgres | UTF8     | C       | C     |
(4 rows)

test=#

 

5.PostgreSQLサーバーの設定変更

5-1.postgresユーザーのパスワードを設定します。

※この後、pg_hba.confファイルの設定を変更すると、PostgreSQLにpostgresユーザーで接続する際にパスワードが必要になります。

test=# alter role postgres with password 'postgres';

ALTER ROLE

test=# \q

[postgres@xxxxxx ~]$

5-2.PostgreSQLの接続方法の変更を行います。

[postgres@xxxxxx ~]$ vi /var/pgsql/data/pg_hba.conf

以下の設定を変更してください。

# PostgreSQL Client Authentication Configuration File
# ===================================================
#

(中略)

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust ←"trust"を"md5"に変更
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust ←"trust"を"md5"に変更
# IPv6 local connections:
host    all             all             ::1/128                 trust ←"trust"を"md5"に変更
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     postgres                                trust
#host    replication     postgres        127.0.0.1/32            trust
#host    replication     postgres        ::1/128                 trust

編集が完了したら、viコマンドの":wq"(保存して閉じる)を実行してください。
また、次のコマンドで保存した内容を反映してPostgreSQLを再起動します。

[postgres@xxxxxx ~]$ pg_ctl -D /var/pgsql/data reload

server signaled

ここまでで、PostgreSQLのインストールは完了です。

 

6.PostgreSQLサーバーの自動起動設定

サーバーのリブート時などに、PostgreSQLデータベースが自動的に起動するように設定しましょう。

PostgreSQLのパッケージソースに同梱されている、自動起動スクリプトテンプレートを流用して、お使いのサーバーで自動起動できるように設定します。

[postgres@xxxxxx ~]$ exit

[root@xxxxxx postgres]# cd /usr/local/src/postgresql-11.10/contrib/start-scripts

[root@xxxxxx start-scripts]# cp linux /etc/init.d/postgresql

[root@xxxxxx start-scripts]# chmod 755 /etc/init.d/postgresql

[root@xxxxxx start-scripts]#

コピーした自動起動スクリプトを、お使いのサーバー環境に合わせて編集します。

[root@xxxxxx start-scripts]# vi /etc/init.d/postgresql

 

ここでは、/var/pgsql/dataにデータベースファイルを作成しましたので、以下の赤字の設定を"/var/pgsql/data"に変更してください。

#! /bin/sh

# chkconfig: 2345 98 02
# description: PostgreSQL RDBMS

 ・

 ・(中略)

 ・

## EDIT FROM HERE


# Installation prefix
prefix=/usr/local/pgsql

 

# Data directory
PGDATA="/usr/local/pgsql/data"  ←"/usr/local"を"/var"に変更

 

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

 

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

 ・

 ・

編集が完了したら、viコマンドの":wq"(保存して閉じる)を実行してください。

 

次に"chkconfig"コマンドで、自動起動スクリプトをシステムに登録します。

[root@xxxxxx start-scripts]# chkconfig --add postgresql

[root@xxxxxx start-scripts]# chkconfig --list | grep postgresql

postgresql 0:off   1:off   2:on   3:on   4:on   5:on   6:off

 

以上で、PostgreSQLの自動起動の設定は完了です。

 

続いて『Redisのインストール』を行います。

 

記載の製品名および商品名は、各社の商標、または登録商標です。