CentOS 7
Sponsored Link

PostgreSQL 10 : インストール2020/01/24

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

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

[root@www ~]#
scl enable rh-postgresql10 bash
[root@www ~]#
postgres -V

postgres (PostgreSQL) 10.6
[root@www ~]#
which postgres

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

source /opt/rh/rh-postgresql10/enable
export X_SCLS="`scl enable rh-postgresql10 'echo $X_SCLS'`"
[4] PostgreSQL 10 の初回セットアップと起動を実施します。
[root@www ~]#
postgresql-setup --initdb --unit rh-postgresql10-postgresql

 * Initializing database in '/var/opt/rh/rh-postgresql10/lib/pgsql/data'
 * Initialized, logs are in /var/lib/pgsql/initdb_rh-postgresql10-postgresql.log

[root@www ~]#
systemctl enable --now rh-postgresql10-postgresql

[5] デフォルト設定では、ローカルホストからのみ接続可能 且つ ローカル接続は [peer] 認証のみとなっています。 認証方式の詳細は公式ドキュメントを参照ください。
⇒ https://www.postgresql.jp/document/10/html/auth-pg-hba-conf.html
# ローカルホストをリスン

[root@www ~]#
grep listen_addresses /var/opt/rh/rh-postgresql10/lib/pgsql/data/postgresql.conf

#listen_addresses = 'localhost' # what IP address(es) to listen on;
# 認証方式

[root@www ~]#
grep -v -E "^#|^$" /var/opt/rh/rh-postgresql10/lib/pgsql/data/pg_hba.conf

local   all             all                                     peer
host    all             all             127.0.0.1/32            ident
host    all             all             ::1/128                 ident
local   replication     all                                     peer
host    replication     all             127.0.0.1/32            ident
host    replication     all             ::1/128                 ident
[6] [peer] 認証の場合、任意の PostgreSQL ユーザーを追加して利用するには、同名の OS ユーザーも必要になります。
# OS ユーザー追加

[root@www ~]#
useradd cent
# PostgreSQL 管理ユーザーで PostgreSQL ユーザーとデータベース追加

[root@www ~]#
su - postgres

-bash-4.2$
createuser cent

-bash-4.2$
createdb testdb -O cent
# 確認

-bash-4.2$
psql -c "select usename from pg_user;"
 usename
----------
 postgres
 cent
(2 rows)

-bash-4.2$
psql -l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 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)
[7] 新規追加したユーザーで PostgreSQL を利用する場合の基本操作です。
# テストDBに接続

[cent@www ~]$
psql testdb

psql (10.6)
Type "help" for help.

# ユーザーロール一覧を表示
testdb=> \du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 cent      |                                                            | {}
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

# データベース一覧を表示
testdb=> \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 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)

# テストテーブルを作成
testdb=> create table test_table (no int, name text); 
CREATE TABLE

# テーブル一覧を表示
testdb=> \dt 
          List of relations
 Schema |    Name    | Type  | Owner
--------+------------+-------+-------
 public | test_table | table | cent
(1 row)

# テストテーブルにテストデータを挿入
testdb=> insert into test_table (no,name) values (01,'CentOS'); 
INSERT 0 1

# 確認
testdb=> select * from test_table; 
 no |  name
----+--------
  1 | CentOS
(1 row)

# テストテーブルを削除
testdb=> drop table test_table; 
DROP TABLE

testdb=> \dt 
Did not find any relations.

# exit する
testdb=> \q 

# テストデータベースを削除

[cent@www ~]$
dropdb testdb

[cent@www ~]$
psql -l

                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 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
(3 rows)
関連コンテンツ