CentOS 8にPostgreSQL 12をインストール（AppStream）

# はじめに

Application Stream(AppStream)を利用してCentOS8にPostgreSQL 12をインストール
親記事：そのうち作成予定
参考：[RHEL8のパッケージ構成 - BaseOSとApplication Stream - 赤帽エンジニアブログ]( https://rheb.hatenablog.com/entry/rhel8_baseos_and_appstream)

## サポート

本手法で導入した場合、[Red Hat Enterprise Linux 8 Application Streams Life Cycle - Red Hat Customer Portal](https://access.redhat.com/node/4079021)より、~~2025-02~~ 2029-05がEOLだと思われる。
それ以降に報告された脆弱性や不具合への対応は実施されない可能性がある。
なおCentOS Linux 8が2021-12にEOLを迎えるため、それ以降の更新を受けるためにはCentOS Stream8を含めた他ディストリビューションへの移行が必要。

## LOG

<details><summary>強制的にインストールしたケース（没版）</summary><div>

### インストール（没版）

AppStreamで提供されている他バージョンと同じ手順でインストールできなかったパターン。
後にLocateのエラー停止やyum updateしておくことで以前と同じ手順で動くことを確認できた。

```shell-session
# cat /etc/redhat-release
CentOS Linux release 8.1.1911 (Core)

# yum module list postgresql
Failed to set locale, defaulting to C.UTF-8
Last metadata expiration check: 0:00:13 ago on Tue Feb 11 01:24:28 2020.
CentOS-8 - AppStream
Name                 Stream           Profiles                    Summary
postgresql           9.6              client, server [d]          PostgreSQL server and client module
postgresql           10 [d]           client, server [d]          PostgreSQL server and client module
postgresql           12               client, server              PostgreSQL server and client module

Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

# yum install -y @postgresql:12/server
... 略

# mkdir /usr/local/pgsql
# chown postgres:postgres /usr/local/pgsql
# ls -la /usr/local/pgsql
total 16
drwxr-xr-x. 2 postgres postgres 4096 Feb 11 01:37 .
drwxr-xr-x. 1 root     root     4096 Feb 11 01:37 ..

# su - postgres

$ initdb --no-local -D /usr/local/pgsql/data
... 略
Success. You can now start the database server using:

    pg_ctl -D /usr/local/pgsql/data -l logfile start
```

### postgresql起動/停止（没版）

```shell-session
$ pg_ctl -D /usr/local/pgsql/data -l logfile start
waiting for server to start.... done
server started
$ pg_ctl -D /usr/local/pgsql/data -l logfile status
pg_ctl: server is running (PID: 410)
/usr/bin/postgres "-D" "/usr/local/pgsql/data"
```

```shell-session
$ pg_ctl -D /usr/local/pgsql/data -l logfile stop
waiting for server to shut down.... done
server stopped
$ pg_ctl -D /usr/local/pgsql/data -l logfile status
pg_ctl: no server running
```

</div></details>

### インストール

```shell-session
# cat /etc/redhat-release
CentOS Linux release 8.1.1911 (Core)

# yum module list postgresql
Last metadata expiration check: 0:05:33 ago on Tue 11 Feb 2020 02:09:01 AM UTC.
CentOS-8 - AppStream
Name                 Stream           Profiles                    Summary
postgresql           9.6              client, server [d]          PostgreSQL server and client module
postgresql           10 [d]           client, server [d]          PostgreSQL server and client module
postgresql           12               client, server              PostgreSQL server and client module


Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

# yum update -y
# yum install -y @postgresql:12/server

... 略

# /usr/bin/postgresql-setup --initdb
... 略
```

### postgresql起動/停止

```shell-session
# systemctl start postgresql
# systemctl status postgresql
● postgresql.service - PostgreSQL database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2020-02-11 02:10:29 UTC; 3s ago
  Process: 421 ExecStartPre=/usr/libexec/postgresql-check-db-dir postgresql (code=exited, status=0/SUCCESS)
 Main PID: 423 (postmaster)
    Tasks: 8 (limit: 8964)
   Memory: 17.0M
   CGroup: /docker/69b522087b2db5e428dca4eb4b77549a111fde99a7f80b83563dbec354d85bfb/docker/69b522087b2db5e428>
           tq423 /usr/bin/postmaster -D /var/lib/pgsql/data
           tq424 postgres: logger
           tq426 postgres: checkpointer
           tq427 postgres: background writer
           tq428 postgres: walwriter
           tq429 postgres: autovacuum launcher
           tq430 postgres: stats collector
           mq431 postgres: logical replication launcher

Feb 11 02:10:29 69b522087b2d systemd[1]: Starting PostgreSQL database server...
Feb 11 02:10:29 69b522087b2d systemd[1]: Started PostgreSQL database server.
```

```shell-session
# systemctl stop postgresql
# systemctl status postgresql
● postgresql.service - PostgreSQL database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql.service; disabled; vendor preset: disabled)
   Active: inactive (dead)

Feb 11 02:10:29 69b522087b2d systemd[1]: Starting PostgreSQL database server...
Feb 11 02:10:29 69b522087b2d systemd[1]: Started PostgreSQL database server.
Feb 11 02:11:08 69b522087b2d systemd[1]: Stopping PostgreSQL database server...
Feb 11 02:11:08 69b522087b2d systemd[1]: Stopped PostgreSQL database server.
```

### postgresql自動起動設定/設定解除

```shell-session
# systemctl enable postgresql
Created symlink /etc/systemd/system/multi-user.target.wants/postgresql.service → /usr/lib/systemd/system/postgresql.service.

# systemctl list-unit-files --type=service |grep postgresql
postgresql.service                     enabled
postgresql@.service                    disabled
```

```shell-session
# systemctl disable postgresql
Removed /etc/systemd/system/multi-user.target.wants/postgresql.service.

# systemctl list-unit-files --type=service |grep postgresql
postgresql.service                          disabled
postgresql@.service                         disabled
```

### 各種確認

```shell-session
# which postgres
/usr/bin/postgres

# which psql
/usr/bin/psql

# psql --version
psql (PostgreSQL) 12.1
```