PostgreSQLはバージョンアップが早く、CentOSの標準リポジトリのみに依存していると、本家でEOLになっていって悲しいので、yumで新しいバージョンのPostgreSQL環境を構築するためのメモ。
ちなみに、CentOS 6ではv8.4.20、CentOS 7ではv9.2.24、CentOS 8ではv10.6が標準で入ります。
PostgreSQL公式のyumリポジトリ
PostgreSQLに関しては、本家がyumリポジトリを提供してくれています。
- PostgreSQL RPM Repository (with Yum)
こちらを参照して、まずはリポジトリを追加します。
# yum -y install <上記のURL>
PostgreSQLパッケージの確認
まずは利用可能なバージョンを確認してみます。
- CentOS 6
# yum list 'postgresql*' | grep '^postgresql[0-9]*[.]' postgresql.x86_64 8.4.20-8.el6_9 @updates postgresql.i686 8.4.20-8.el6_9 base postgresql10.x86_64 10.13-1PGDG.rhel6 pgdg10 postgresql11.x86_64 11.8-1PGDG.rhel6 pgdg11 postgresql12.x86_64 12.3-1PGDG.rhel6 pgdg12 postgresql95.x86_64 9.5.22-1PGDG.rhel6 pgdg95 postgresql96.x86_64 9.6.18-1PGDG.rhel6 pgdg96
- CentOS 7
# yum list 'postgresql*' | grep '^postgresql[0-9]*[.]' postgresql.x86_64 9.2.24-1.el7_5 @updates postgresql.i686 9.2.24-4.el7_8 updates postgresql.x86_64 9.2.24-4.el7_8 updates postgresql10.x86_64 10.13-1PGDG.rhel7 pgdg10 postgresql11.x86_64 11.8-1PGDG.rhel7 pgdg11 postgresql12.x86_64 12.3-5PGDG.rhel7 pgdg12 postgresql95.x86_64 9.5.22-1PGDG.rhel7 pgdg95 postgresql96.x86_64 9.6.18-1PGDG.rhel7 pgdg96
- CentOS 8
# yum list 'postgresql*' | grep '^postgresql[0-9]*[.]' postgresql.x86_64 10.6-1.module_el8.0.0+15+f57f353b @AppStream
あれ、CentOS 8だけ、標準版以外が見つかりません。もう一度。
# less /etc/yum.repos.d/pgdg-redhat-all.repo # yum list --disablerepo='AppStream' --enablerepo='pgdg*' 'postgresql*' | grep '^postgresql[0-9]*[.]' postgresql.x86_64 10.6-1.module_el8.0.0+15+f57f353b @AppStream postgresql10.src 10.13-1PGDG.rhel8 pgdg10-source postgresql10.x86_64 10.13-1PGDG.rhel8 pgdg10 postgresql11.src 11.8-2PGDG.rhel8 pgdg11-source postgresql11.src 11.8-2PGDG.rhel8 pgdg11-source-updates-testing postgresql11.x86_64 11.8-2PGDG.rhel8 pgdg11 postgresql11.x86_64 11.8-2PGDG.rhel8 pgdg11-updates-testing postgresql12.src 12.3-5PGDG.rhel8 pgdg12-source postgresql12.src 12.3-5PGDG.rhel8 pgdg12-source-updates-testing postgresql12.x86_64 12.3-5PGDG.rhel8 pgdg12 postgresql12.x86_64 12.3-5PGDG.rhel8 pgdg12-updates-testing postgresql13.x86_64 13-beta1_2PGDG.rhel8 pgdg13-updates-testing postgresql94.src 9.4.26-1PGDG.rhel8 pgdg94-source postgresql94.x86_64 9.4.26-1PGDG.rhel8 pgdg94 postgresql95.src 9.5.22-1PGDG.rhel8 pgdg95-source postgresql95.x86_64 9.5.22-1PGDG.rhel8 pgdg95 postgresql96.src 9.6.18-1PGDG.rhel8 pgdg96-source postgresql96.x86_64 9.6.18-1PGDG.rhel8 pgdg96
あれ、「enabled=1」になっているんですけどね。
ちなみに、CentOS 6/7/8ともに、pgdg94は「enabled=0」になっています。念のため。
PostgreSQL環境のインストール
それでは、気を取り直して、PostgreSQL v12をインストールしてみます。
- CentOS 6/7
# yum -y install postgresql12-server
- CentOS 8
# yum -y install --disablerepo='AppStream' --enablerepo='pgdg12' postgresql12-server
ちなみに、CentOS 7でのインストール中に以下のようなエラーが出ますが、今回は無視します。標準版が入っているせいなので。
シンボリックリンク /usr/bin/psql -> /etc/alternatives/pgsql-psql の作成に失敗しました。 /usr/bin/psql がすでに存在しており、シンボリックリンクファイル ではありません。 : :
- CentOS 6/7/8
# /usr/pgsql-12/bin/psql --version psql (PostgreSQL) 12.3
とりあえずOKそうです。
環境設定
- CentOS 6
# service postgresql-12 initdb データベースを初期化中: [ OK ] # vim /var/lib/pgsql/12/data/postgresql.conf # vim /var/lib/pgsql/12/data/pg_hba.conf # service postgresql-12 start postgresql-12 サービスを開始中: [ OK ] # /usr/pgsql-12/bin/psql -U postgres psql (12.3) "help"でヘルプを表示します。 postgres=# \l データベース一覧 名前 | 所有者 | エンコーディング | 照合順序 | Ctype(変換演算子) | アクセス権限 -----------+----------+------------------+-------------+-------------------+----------------------- postgres | postgres | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | template0 | postgres | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres (3 行) postgres=# \q
- CentOS 7/8
# /usr/pgsql-12/bin/postgresql-12-setup initdb Initializing database ... OK # vim /var/lib/pgsql/12/data/postgresql.conf # vim /var/lib/pgsql/12/data/pg_hba.conf # systemctl start postgresql-12 # /usr/pgsql-12/bin/psql -U postgres psql (12.3) "help"でヘルプを表示します。 postgres=# \l データベース一覧 名前 | 所有者 | エンコーディング | 照合順序 | Ctype(変換演算子) | アクセス権限 -----------+----------+------------------+------------+-------------------+----------------------- postgres | postgres | UTF8 | ja_JP.utf8 | ja_JP.utf8 | template0 | postgres | UTF8 | ja_JP.utf8 | ja_JP.utf8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | ja_JP.utf8 | ja_JP.utf8 | =c/postgres + | | | | | postgres=CTc/postgres (3 行) postgres=# \q
参考
- CentOS 6 に PostgreSQL 9.x を yum インストールする手順