CentOS 6 に PostgreSQL 8.4 をインストール

家でPostgreSQLの検証用の環境が欲しかったので、Vagrantで環境作りました。

まずは8.4を作りましたが、同じように9.0、9.1、9.3、9.4まで作る予定です。(手順はRPMのリポジトリが違うくらい)

Vagrantで仮想環境作成

box は chef/centos-6.5 を使います。

D:\vagrant>mkdir postgres
D:\vagrant>cd postgres
D:\vagrant\postgres>vagrant init chef/centos-6.5
D:\vagrant\postgres>notepad Vagrantfile
D:\vagrant\postgres>vagrant up

RPMのリポジトリ登録

PostgreSQLの公式サイトで提供されているRPMのリポジトリを使用します。

[root@localhost ~]# wget http://yum.postgresql.org/8.4/redhat/rhel-6-x86_64/pgdg-centos-8.4-3.noarch.rpm
[root@localhost ~]# rpm -ivh pgdg-centos-8.4-3.noarch.rpm
[root@localhost ~]# yum repolist
repo id                        repo name                                        status
base                           CentOS-6 - Base                                  6,518
extras                         CentOS-6 - Extras                                   37
pgdg84                         PostgreSQL 8.4 6 - x86_64                          155
updates                        CentOS-6 - Updates                                 830
repolist: 7,540

PostgreSQLのインストール

PostgreSQLをインストールして、初期化とサービスの登録を行います。

[root@localhost ~]# yum install -y postgresql84-server
[root@localhost ~]# chkconfig postgresql-8.4 on
[root@localhost ~]# chkconfig --list postgresql-8.4
postgresql-8.4  0:off   1:off   2:on    3:on    4:on    5:on    6:off
[root@localhost ~]# /etc/init.d/postgresql-8.4 initdb
Initializing database:
                                                           [  OK  ]
[root@localhost ~]# /etc/init.d/postgresql-8.4 start
Starting postgresql-8.4 service:                           [  OK  ]

テスト用のユーザ&DB作成

テスト用のユーザとして"testuser"を作成し、"test"というDBを作成します。

[root@localhost ~]# su - postgres
-bash-4.1$ psql
psql (8.4.22)
"help" でヘルプを表示します.
postgres=# CREATE ROLE testuser WITH CREATEDB LOGIN PASSWORD 'testuser';
postgres=# \q
-bash-4.1$ logout
[root@localhost ~]# useradd testuser
[root@localhost ~]# su - testuser
[testuser@localhost ~]$ createdb test
[testuser@localhost ~]$ psql test
psql (8.4.22)
"help" でヘルプを表示します.
test=> \d
リレーションがありません。
test=> CREATE TABLE users (
test(>   id integer PRIMARY KEY,
test(>   name text NOT NULL
test(> );
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "users_pkey" for table "users"
CREATE TABLE
test=> INSERT INTO users VALUES(1, 'user1');
INSERT 0 1
test=> SELECT * FROM users;
 id | name
----+-------
  1 | user1
(1 行)