PostgreSQLをインストール

1.前提

CentOS release 6.9 (Final)  64bit

 

2.インストール操作

(1)PostgreSQLのyumリポジトリを追加

# yum -y localinstall https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-6-x86_64/pgdg-centos96-9.6-3.noarch.rpm

 

※PostgreSQLが提供するyumリポジトリは、下記で確認できる。

 https://yum.postgresql.org/repopackages.php

 

(2)利用できるPostgreSQLの確認

下記コマンドを実行して、利用できるソフトウェアを確認する。

 

# yum list postgres*

 

(3)PostgreSQLのインストール

 

# yum -y install postgresql96-server.x86_64

 

(4)初期化

# su - postgres

 

$ /usr/pgsql-9.6/bin/initdb -E UTF-8 --no-locale -D /var/lib/pgsql/9.6/data

$ exit

 

(5)起動(と停止)

# service postgresql-9.6 start

 

# service postgresql-9.6 stop

※停止は、必要な場合に実施すること。

 

(6)起動設定

 

下記コマンドで、デフォルトで起動するようにする。

 

# chkconfig postgresql-9.6 on

 

(7)DBのpostgresユーザにパスワードの設定

 

# su - postgres

 

$ psql

postgres=# ALTER USER postgres PASSWORD 'new_password';

postgres=# \q

$ exit

 

(8)設定変更

(a)他ホストからの接続を許容する

 

# vi /var/lib/pgsql/9.6/data/postgresql.conf

 

--------------------------------

listen_addresses = '*'

--------------------------------

59行目を 'localhost' -> '*' に変更し、他ホストからの接続を許可する。

 

(b)DB名とユーザ名が一致しなくても認証できるようにする

 

# vi /var/lib/pgsql/9.6/data/pg_hba.conf

 

<変更前>

--------------------------------

local   all             all                                     trust

--------------------------------

 

<変更後>

--------------------------------

local   all             all                                     md5

--------------------------------

 

(c)設定反映と確認

 

# service postgresql-9.6 restart

 

# su - postgres

 

$ psql

パスワード:

psql (9.6.5)

 

→ログインできることを確認する。

 

(9)テストユーザとDBの作成

(a)DBユーザの追加

 

$ createuser -P testuser

 

(b)testdbの作成とDB所有者の変更

・DBの追加

 $ psql

 postgres=# create database testdb;

 CREATE DATABASE

 

・DB所有者の変更

 postgres=# ALTER DATABASE testdb OWNER TO testuser;

 postgres=# \q

(c)テストテーブルの作成

 

$ psql -d testdb -U testuser

 

testdb=> create table test (id int, name text);

CREATE TABLE

testdb=> \d

          リレーションの一覧

 スキーマ | 名前 |    型    |  所有者

----------+------+----------+----------

 public   | test | テーブル | testuser

(1 行)

 

testdb=> insert into test(id, name) values(1, 'テスト');

 

testdb=> select * from test;

 

→ testテーブルにデータの挿入と検索ができる。

 

写真素材のピクスタ