21
10

More than 5 years have passed since last update.

RPMを用いたPostgreSQL 9.5 のインストール手順

Last updated at Posted at 2016-05-26

PostgreSQLが少しでも広まってくれると嬉しいなと思い、色々書いていきます。
まずはインストールからDB構築まで。

最初に

ここでは、PostgreSQL 9.5.3をCentOS 7 にRPMを用いてインストールする手順を説明します。

1. RPMファイルを準備する

PostgreSQLコミュニティがRPMファイルを配布しているので、それを使用します。

今回、PostgreSQL構築で使用するファイルは以下のとおりです。

  • postgresql95-9.5.3-2PGDG.rhel7.x86_64.rpm
  • postgresql95-server-9.5.3-2PGDG.rhel7.x86_64.rpm
  • postgresql95-libs-9.5.3-2PGDG.rhel7.x86_64.rpm
  • postgresql95-devel-9.5.3-2PGDG.rhel7.x86_64.rpm
  • postgresql95-contrib-9.5.3-2PGDG.rhel7.x86_64.rpm
パッケージ名 概要
postgresql PostgreSQLに接続や操作等を行うためのクライアントプログラム
postgresql-server PostgreSQLサーバ作成、起動/停止等に必要なプログラム
postgresql-libs クライアントプログラムやI/Fに必要なPostgreSQLのライブラリ群
postgresql-devel 開発モジュール。CやC++アプリケーションのコンパイルに必要なヘッダファイル、ライブラリ
postgresql-contrib PostgreSQLが提供する追加モジュール

ちなみにyumでインストールする方法もありますが、Let's PostgreSQLさんの記事が参考になりますのでご確認ください。

2. RPMパッケージをインストールする

# rpm -ivh ./rpm/postgresql95-*
warning: ./rpm/postgresql95-9.5.3-2PGDG.rhel7.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 442df0f8: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:postgresql95-libs-9.5.3-2PGDG.rhe################################# [ 17%]
   2:postgresql95-9.5.3-2PGDG.rhel7   ################################# [ 33%]
   3:postgresql95-contrib-9.5.3-2PGDG.################################# [ 50%]
   4:postgresql95-devel-9.5.3-2PGDG.rh################################# [ 67%]
   5:postgresql95-server-9.5.3-2PGDG.r################################# [ 83%]
   6:postgresql95-debuginfo-9.5.3-2PGD################################# [100%]

3. PostgreSQLの起動方法

3-1. ~/.bash_profileを編集

~/.bash_profileに以下の内容を記載する。

export PATH=/usr/pgsql-9.5/bin:$PATH
export PGDATA=/var/lib/pgsql/9.5/data

上記で編集した内容を反映する。

$ source ~/.bash_profile

3-2. DBクラスタ作成

PostgreSQLのDBクラスタを作成する。

$ initdb --no-locale --encoding=utf-8
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "C".
・・・

3-3. 設定ファイルを編集

本来は、システムの要件に応じて設定ファイル(postgresql.conf)を変更する必要があります。
今回は手元の環境でも、ここだけは変えておいたほうが使いやすいというものをお伝えします。

$PGDATA/postgresql.conf
# Add settings for extensions here
listen_addresses = '*'
wal_level = hot_standby
checkpoint_timeout = 15min
logging_collector = on
log_filename = 'postgresql-%Y-%m-%d.log'
log_min_duration_statement = 60
log_checkpoints = on
log_lock_waits = on 
log_error_verbosity = verbose
log_line_prefix = '%m [%p] '

今回はローカル接続のみしか考慮しないのでpg_hba.confはそのまま使用しています。

3-4. DB起動

PostgreSQLを起動します。

$ pg_ctl start
server starting
-bash-4.2$ 2016-05-27 00:46:38.824 JST [16487] LOG:  00000: redirecting log output to logging collector process
2016-05-27 00:46:38.824 JST [16487] HINT:  Future log output will appear in directory "pg_log".
2016-05-27 00:46:38.824 JST [16487] LOCATION:  SysLogger_Start, syslogger.c:622

4. 起動の確認

プロセスを確認し、以下のプロセスがあることをチェックします。

$ ps -ef | grep postgres
postgres 16487     1  0 00:46 pts/1    00:00:00 /usr/pgsql-9.5/bin/postgres
postgres 16488 16487  0 00:46 ?        00:00:00 postgres: logger process   
postgres 16490 16487  0 00:46 ?        00:00:00 postgres: checkpointer process  
postgres 16491 16487  0 00:46 ?        00:00:00 postgres: writer process   
postgres 16492 16487  0 00:46 ?        00:00:00 postgres: wal writer process  
postgres 16493 16487  0 00:46 ?        00:00:00 postgres: autovacuum launcher process  
postgres 16494 16487  0 00:46 ?        00:00:00 postgres: stats collector process  

プロセスが上がっていなければ、$PGDATA/pg_log/postgresql-*.logを確認してください。
正しく起動していれば、以下のログが出ているはずです。

postgresql-*.log
2016-05-27 00:46:38.863 JST [16487] LOG:  00000: database system is ready to accept connections

5. 動作確認(テーブル作成、データ挿入)

お試しにテスト用のテーブルを作成して、データをINSERTしてみます。

$ psql postgres -c "CREATE TABLE bar(i int)"
CREATE TABLE
$ psql postgres -c "INSERT INTO bar VALUES (1)"
INSERT 0 1

INSERTしたデータがSELECTできることを確認します。

$ psql postgres -c "SELECT * FROM bar"
 i 
---
 1
(1 row)

ちゃんと構築できましたね。
次回はレプリケーション構成(同期、非同期)について書こうと思います。

21
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
10