PostgreSQL 9.6 のインストール方法 (CentOS)

OSS のデータベース “PostgreSQL”

みなさまこんにちは
(株)EIT技術ブログです。

今回は PostgreSQL のお話しをさせて頂きます。

PostgreSQL は OSS のデータベースですが、近年商用環境での利用が増加しているとの話も多く耳にします。

商用サポートのある Red Hat Enterprise Linux を始め様々な分野で Linux OS が浸透していますが、最近は PostgreSQL のような Linux 以外の OSS 製品も商用環境にて利用されている場面が増えてきているのではないでしょうか。

弊社も流行に乗り遅れないよう OSS に注目しているのですが、その中で私も最近 PostgreSQL に触れる機会がありました。

手始めにインストールから試みたのですが、やはり新しい製品を扱う際は何かと躓いたりすることもありましたので、私自身の備忘録も兼ねて PostgreSQL のインストール方法を紹介しようと思います。

今回の記事における環境は以下の通りです。

OS : CentOS 7.3
PostgreSQL : 9.6.2

 

PostgreSQL のインストール方法

さて、インストール方法についてですが、前述の通り今回は CentOS を利用しているため、パッケージ管理ツール (yum) を使用する方法とソースコードからインストールする方法の 2種類の方法があります。

それぞれの方法にて手順が異なる部分はありますが、大まかな流れは以下のようになります。

PostgreSQL (実行ファイル等) のインストール
・データベースクラスタの作成
PostgreSQL の起動

 

ということで、それぞれの方法について紹介していきます。

※以下に紹介する手順は、なるべく必要な手順のみに絞ったシンプルな方法です。環境変数の設定やオプションの説明等割愛している部分も多いですので、開発コミュニティ (postgresql.org) が公開しているドキュメントや日本ユーザ会 (postgresql.jp) が公開している日本語ドキュメント等を是非参照してみて下さい。

 

yum を使用したインストール

まずは yum を利用した方法を紹介します。

 

1. リポジトリの登録

以下のコマンドにて、PostgreSQL の開発コミュニティが公開しているリポジトリ rpm を入手し、登録します。

[root@localhost ~]# wget https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm
[root@localhost ~]# yum install pgdg-centos96-9.6-3.noarch.rpm -y

上記コマンドを実行すると、/etc/yum.repos.d/ 配下に pgdg-96-centos.repo ファイルが格納されます。

[root@localhost ~]# ls -l /etc/yum.repos.d/pgdg-96-centos.repo
-rw-r--r--. 1 root root 1012 Sep 21 2016 /etc/yum.repos.d/pgdg-96-centos.repo

 

2. PostgreSQL インストール

以下の yum コマンドにて、PostgreSQL をインストールします。

[root@localhost ~]# yum install postgresql96-server -y

インストールが完了すると、/usr/pgsql-9.6/ 配下に実行ファイル等がインストールされます。

[root@localhost ~]# ls -l /usr/pgsql-9.6/
total 12
drwxr-xr-x. 2 root root 4096 Apr 4 08:46 bin
drwxr-xr-x. 2 root root 4096 Apr 4 08:46 lib
drwxr-xr-x. 7 root root 4096 Apr 4 08:46 share

また、PostgreSQL 実行用のユーザ postgres や、デフォルトのデータベースクラスタ格納先のディレクトリである /var/lib/pgsql/9.6/data/ も作成されます。

[root@localhost ~]# id postgres
uid=26(postgres) gid=26(postgres) groups=26(postgres)
[root@localhost ~]# ls -ld /var/lib/pgsql/9.6/data/
drwx------. 20 postgres postgres 4096 Apr 4 17:07 /var/lib/pgsql/9.6/data/

 

3. データベースクラスタ作成

CentOS の yum コマンドにて PostgreSQL をインストールした直後の状態では、実行ファイル等がインストールされているのみであり、データベースの実体となるファイルは存在していません。

データベースの実体となるファイル等が格納されているディレクトリを PostgreSQL では “データベースクラスタ” と呼びますが、インストール後にこのデータベースクラスタを作成する必要があります。

まずは、ユーザを postgres ユーザに切り替えます。

[root@localhost ~]# su - postgres

postgres ユーザにて環境変数 PGDATA を確認すると、データベースクラスタ格納先ディレクトリが設定されていることが確認できます。

-bash-4.2$ env | grep PGDATA
PGDATA=/var/lib/pgsql/9.6/data

※PGDATA 等の環境変数のお話しは割愛します。

環境変数が設定されていることが確認できたら、initdb コマンドを使ってデータベースクラスタを作成します。

-bash-4.2$ /usr/pgsql-9.6/bin/initdb --encoding=utf8 --no-locale

以下のようなメッセージが出力されれば完了です。

Success. You can now start the database server using:
/usr/pgsql-9.6/bin/pg_ctl -D /var/lib/pgsql/9.6/data -l logfile start

 

4. PostgreSQL 起動

データベースクラスタの作成が完了したら PostgreSQL を起動してみましょう。

CentOS の yum を使ってインストールした場合、systemd を使用して起動/停止等を行うためのユニットファイルが併せてインストールされているため、そちらを使って起動/停止することが可能です。

以下のコマンドにて、PostgreSQL を起動します。

※root 権限が必要であるため、root ユーザのパスワードを入力する必要があります。

-bash-4.2$ systemctl start postgresql-9.6.service
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to manage system services or units.
Authenticating as: root
Password:
==== AUTHENTICATION COMPLETE ===

上記コマンド実行後、以下のコマンドにて PostgreSQL が起動していることを確認します。

-bash-4.2$ systemctl status postgresql-9.6.service
● postgresql-9.6.service - PostgreSQL 9.6 database server
Loaded: loaded (/usr/lib/systemd/system/postgresql-9.6.service; disabled; vendor preset: disabled)
Active: active (running) since 火 2017-04-04 08:56:22 JST; 7s ago
Process: 3622 ExecStartPre=/usr/pgsql-9.6/bin/postgresql96-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
Main PID: 3629 (postmaster)
CGroup: /system.slice/postgresql-9.6.service
├─3629 /usr/pgsql-9.6/bin/postmaster -D /var/lib/pgsql/9.6/data/
├─3633 postgres: logger process
├─3635 postgres: checkpointer process
├─3636 postgres: writer process
├─3637 postgres: wal writer process
├─3638 postgres: autovacuum launcher process
└─3639 postgres: stats collector process

“Active:” 欄に “active (running)” と表示されていれば起動しています。

最後に、クライアントインターフェース (psql) を使用してデータベースにアクセスしてみましょう。

以下のコマンドにてデータベースに接続します。

-bash-4.2$ /usr/pgsql-9.6/bin/psql
psql (9.6.2)
"help" でヘルプを表示します.
postgres=#

正常に接続が完了するとプロンプト “postgres=#” が表示され、SQL や関数を実行することが可能です。

postgres=# select version();
version
----------------------------------------------------------------------------------------------------------
PostgreSQL 9.6.2 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-11), 64-bit
(1 行)

接続を終了する場合は “\q” コマンドを実行します。

postgres=# \q
-bash-4.2$

yum でのインストール方法は以上です。

 

ソースコードからインストール

次にソースコードからインストールする方法を紹介します。

 

1. 事前準備

コンパイルに必要な gcc がインストールされていない場合は、yum コマンドでインストールしておきます。

[root@localhost ~]# yum install gcc -y

また、PostgreSQL のコンパイルに必要となる以下のパッケージがインストールされていない場合も、同様にインストールしておきます。

[root@localhost ~]# yum install readline-devel zlib-devel -y

※readline-devel と zlib-devel が無い状態でも PostgreSQL をコンパイルすることは可能なのですが、推奨されている方法ではないため割愛します。

最後に PostgreSQL のコンパイルや実行に使用するユーザ postgres を作成します。

[root@localhost ~]# useradd postgres

 

2. ソースコードのダウンロード

以下のコマンドにて、ソースコードをダウンロードし解凍します。

※今回は /usr/local/src/ 配下にソースコードを展開して作業します。

[root@localhost ~]# cd /usr/local/src/
[root@localhost src]# wget https://ftp.postgresql.org/pub/source/v9.6.2/postgresql-9.6.2.tar.gz
[root@localhost src]# tar xzf postgresql-9.6.2.tar.gz

解凍が完了し、ファイルが展開されていることを確認します。

[root@localhost src]# ls -l postgresql-9.6.2
total 652
-rw-r--r--. 1 1107 1107 1192 Feb 7 06:45 COPYRIGHT
-rw-r--r--. 1 1107 1107 3638 Feb 7 06:45 GNUmakefile.in
-rw-r--r--. 1 1107 1107 283 Feb 7 06:45 HISTORY
-rw-r--r--. 1 1107 1107 75080 Feb 7 06:59 INSTALL
-rw-r--r--. 1 1107 1107 1489 Feb 7 06:45 Makefile
-rw-r--r--. 1 1107 1107 1209 Feb 7 06:45 README
-rw-r--r--. 1 1107 1107 384 Feb 7 06:45 aclocal.m4
drwxrwxrwx. 2 1107 1107 4096 Feb 7 06:56 config
-rwxr-xr-x. 1 1107 1107 471159 Feb 7 06:45 configure
-rw-r--r--. 1 1107 1107 75197 Feb 7 06:45 configure.in
drwxrwxrwx. 55 1107 1107 4096 Feb 7 06:56 contrib
drwxrwxrwx. 3 1107 1107 107 Feb 7 06:56 doc
drwxrwxrwx. 16 1107 1107 4096 Feb 7 06:59 src

 

3. コンパイル及びインストール

ソースコードの展開が完了したらコンパイルを実行します。

まずは、ユーザを postgres ユーザに切り替えます。

[root@localhost src]# su - postgres
[postgres@localhost ~]$

先ほどソースコードを展開したディレクトリに移動し、configure スクリプトを実行します。

[postgres@localhost ~]$ cd /usr/local/src/postgresql-9.6.2/
[postgres@localhost postgresql-9.6.2]$ ./configure

configure スクリプトにて Makefile が作成されたことを確認します。

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/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

次に make コマンドを実行しコンパイルを実行します。

[postgres@localhost postgresql-9.6.2]$ make

大量のメッセージが出力されますが、最後に以下のメッセージが出力されることを確認します。

All of PostgreSQL successfully made. Ready to install.

コンパイルが完了したら make install コマンドでインストールを実行します。

インストール先ディレクトリ (今回の場合デフォルトのインストール先である /usr/local/pgsql/) に対する書き込み権限が必要であるため root ユーザにて実行します。

[postgres@localhost postgresql-9.6.2]$ su -

コンパイルを実施したディレクトリに移動し、make install コマンドにてインストールを実行します。

[root@localhost ~]# cd /usr/local/src/postgresql-9.6.2/
[root@localhost postgresql-9.6.2]# make install

こちらも大量にメッセージが出力されますが、最後に以下のメッセージが出力されることを確認します。

PostgreSQL installation complete.

インストール先のディレクトリ (今回の場合デフォルトのインストール先である /usr/local/pgsql/) 配下に実行ファイル等がインストールされていることを確認します。

[root@localhost postgresql-9.6.2]# ls -l /usr/local/pgsql/
total 16
drwxr-xr-x. 2 root root 4096 Apr 3 09:01 bin
drwxr-xr-x. 6 root root 4096 Apr 3 09:00 include
drwxr-xr-x. 4 root root 4096 Apr 3 09:01 lib
drwxr-xr-x. 6 root root 4096 Apr 3 09:01 share

インストール完了後、データベースクラスタを格納するディレクトリを作成し、必要な権限設定を実施しておきます。

[root@localhost postgresql-9.6.2]# mkdir /usr/local/pgsql/data
[root@localhost postgresql-9.6.2]# chown postgres:postgres /usr/local/pgsql/data/
[root@localhost postgresql-9.6.2]# chmod 700 /usr/local/pgsql/data/
[root@localhost postgresql-9.6.2]# ls -ld /usr/local/pgsql/data/
drwx------. 2 postgres postgres 6 Apr 4 08:37 /usr/local/pgsql/data/

作業完了後、root ユーザからログアウトします。

[root@localhost postgresql-9.6.2]# exit
[postgres@localhost postgresql-9.6.2]$

 

4. データベースクラスタ作成

yum コマンドにて PostgreSQL をインストールした時と同様に、ソースコードからインストールした場合もデータベースクラスタ作成する必要があります。

initdb コマンドでデータベースクラスタを作成しますが、ソースコードからインストールした場合は postgres ユーザの環境変数に PGDATA は設定されていないため、”-D” オプションでデータベースクラスタを作成するディレクトリを指定します。

[postgres@localhost postgresql-9.6.2]$ /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data --encoding=utf8 --no-locale

以下のようなメッセージが出力されれば完了です。

Success. You can now start the database server using:
/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start

 

5. PostgreSQL 起動

以下のコマンドにて、PostgreSQL を起動します。

[postgres@localhost postgresql-9.6.2]$ /usr/local/pgsql/bin/pg_ctl start -D /usr/local/pgsql/data/ -w -t 60
waiting for server to start....LOG: database system was shut down at 2017-04-04 08:38:44 JST
LOG: MultiXact member wraparound protections are now enabled
LOG: database system is ready to accept connections
LOG: autovacuum launcher started
done
server started

最後に、クライアントインターフェース (psql) を使用してデータベースにアクセスしてみましょう。

[postgres@localhost postgresql-9.6.2]$ /usr/local/pgsql/bin/psql
psql (9.6.2)
Type "help" for help.
postgres=#

正常に接続が完了するとプロンプト “postgres=#” が表示され、SQL や関数を実行することが可能です。

postgres=# select version();
version
----------------------------------------------------------------------------------------------------------
PostgreSQL 9.6.2 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-11), 64-bit
(1 row)

接続を終了する場合は “\q” コマンドを実行します。

postgres=# \q
[postgres@localhost postgresql-9.6.2]$

ソースコードからのインストール方法は以上です。

 

終わりに

さて、今回は PostgreSQL のインストール方法を紹介させて頂きましたが、いかがだったでしょうか。

「yum でのインストールとソースコードからのインストールどちらが良いのか?」というようなお話しもあるようですが、それぞれにメリット/デメリットがあり明確な善し悪しの判断は難しいのではないかと思われます。

今回は PostgreSQL のインストール方法のみのお話でしたが、今後上記のようなインストール方法による違いや、PostgreSQL の仕組み等の情報も発信していけたらなと思いつつ、今回はこの辺で失礼させて頂きます。

最後まで読んで頂いた皆様、ありがとうございました。