CentOS 6
Sponsored Link

PostgreSQL 9.5 : インストール2017/09/25

 
CentOS 6 標準の PostgreSQL のバージョンは 8.4系ですが、9.5系を RPM パッケージでインストールします。
[1] CentOS SCLo Software Collections からインストール可能です。
なお、デフォルトバージョンの 8.4系がインストールされた状態でも、Software Collections パッケージは別パスにインスールされるため、複数バージョンの共存が可能となっています。
# SCLoからインストール

[root@dlp ~]#
yum --enablerepo=centos-sclo-rh -y install rh-postgresql95-postgresql-server
[2] Software Collections パッケージは /opt 配下にインストールされます。
環境変数を読み込んで利用するには以下のように実行します。
# 環境変数を読み込む

[root@dlp ~]#
scl enable rh-postgresql95 bash
[root@dlp ~]#
postgres -V

postgres (PostgreSQL) 9.5.7
[root@dlp ~]#
which postgres

/opt/rh/rh-postgresql95/root/usr/bin/postgres
[3] ログイン時に自動的に有効にするには以下のように設定します。
[root@dlp ~]#
vi /etc/profile.d/rh-postgresql95.sh
# 以下の内容で新規作成

#!/bin/bash

source /opt/rh/rh-postgresql95/enable
export X_SCLS="`scl enable rh-postgresql95 'echo $X_SCLS'`"
[4] PostgreSQL 9.5 を有効にして、管理者ユーザーのパスワード設定、ユーザー登録、データベース作成を実施します。
[root@dlp ~]#
postgresql-setup --initdb --unit rh-postgresql95-postgresql

* Initializing database in '/var/opt/rh/rh-postgresql95/lib/pgsql/data'
* Initialized, logs are in /var/lib/pgsql/initdb_rh-postgresql95-postgresql.log
[root@dlp ~]#
vi /var/opt/rh/rh-postgresql95/lib/pgsql/data/postgresql.conf
# 59行目:他ホストからのアクセスも受け付ける場合はコメント解除して変更

listen_addresses = '
*
'
# 417行目:ログ形式を変更する場合はコメント解除して追記 (以下は [日時 ユーザー DB ~] 形式)

log_line_prefix = '
%t %u %d
'
[root@dlp ~]#
/etc/rc.d/init.d/rh-postgresql95-postgresql start

Starting rh-postgresql95-postgresql service: [ OK ]
[root@dlp ~]#
chkconfig rh-postgresql95-postgresql on

[5] PostgreSQL を他ホストからも利用する場合 且つ IPTables を有効にしている場合は、PostgreSQL サービスポートの許可が必要です。PostgreSQL は 5432/TCP を使用します。なお、「-I INPUT 5」の箇所は自身の環境を確認して、適切な値に置き換えてください。
[root@dlp ~]#
iptables -I INPUT 5 -p tcp -m state --state NEW -m tcp --dport 5432 -j ACCEPT
[6] PostgreSQL 管理者ユーザーのパスワード設定、ユーザー登録、データベース作成を実施します。
# パスワード設定

[root@dlp ~]#
su - postgres

-bash-4.1$
psql -c "alter user postgres with password 'password'"

ALTER ROLE
# DBユーザー [cent] を登録

-bash-4.1$
createuser cent
# テストデータベース作成 (オーナーは上記ユーザー)

-bash-4.1$
createdb testdb -O cent
[7] DB 登録したユーザーでログインし、データベース操作のテストをします。
# 確認

[cent@dlp ~]$
psql -l

                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileg
es
-----------+----------+----------+-------------+-------------+------------------
-----
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres    +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres    +
           |          |          |             |             | postgres=CTc/postgres
 testdb    | cent     | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
(4 rows)

# テストDBに接続

[cent@dlp ~]$
psql testdb

psql (9.5.7)
Type "help" for help.
# パスワード設定

testdb=#
alter user cent with password 'password';

ALTER ROLE
# テストテーブル作成

testdb=#
create table test ( no int,name text );

CREATE TABLE
# テストデータ挿入

testdb=#
insert into test (no,name) values (1,'cent');

INSERT 0 1
# 確認

testdb=#
select * from test;
 no | name
----+-------
  1 | cent
(1 row)
# テストテーブル削除

testdb=#
drop table test;

DROP TABLE
# 終了

testdb=#
\q
# テストDB削除

[cent@dlp ~]$
dropdb testdb
関連コンテンツ