CentOS8 PostgreSQL 12をインストールする
- 作成日 2020.04.28
- 更新日 2023.01.04
- centos8 PostgreSQL
- インスール

centos8にPostgreSQL 12をインスールする手順を記述してます。リポジトリを追加してインストールします。ここではインストール後に外部から接続するまでを掲載してます。
環境
- OS CentOS Linux release 8.0.1905 (Core)
リポジトリ追加
PostgreSQLのリポジトリを追加します。
sudo yum -y install https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm
追加したリポジトリを確認します。
rpm -qi pgdg-redhat-repo
<出力結果>
Name : pgdg-redhat-repo
Version : 42.0
Release : 9
Architecture: noarch
Group : Unspecified
Size : 10213
License : PostgreSQL
Signature : DSA/SHA1, Sun 05 Apr 2020 11:39:28 PM UTC, Key ID 1f16d2e1442df0f8
Source RPM : pgdg-redhat-repo-42.0-9.src.rpm
Build Date : Sun 05 Apr 2020 11:39:28 PM UTC
Build Host : koji-rhel8-x86-64-pgbuild
Relocations : (not relocatable)
Vendor : PostgreSQL Global Development Group
URL : https://yum.postgresql.org
Summary : PostgreSQL PGDG RPMs- Yum Repository Configuration for Red Hat / CentOS
PostgreSQL 12インストール
元々あるPostgreSQLモジュールを無効にしておきます。
sudo dnf -qy module disable postgresql
インストールします。
sudo dnf -y install postgresql12 postgresql12-server
起動と自動起動を設定しておきます。
sudo systemctl enable --now postgresql-12
ステータスを確認します。
systemctl status postgresql-12
<出力結果>
● postgresql-12.service - PostgreSQL 12 database server
Loaded: loaded (/usr/lib/systemd/system/postgresql-12.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code)
Docs: https://www.postgresql.org/docs/12/static/
Process: 12726 ExecStartPre=/usr/pgsql-12/bin/postgresql-12-check-db-dir ${PGDATA} (code=exited, status=1/FAILURE)
failedになってました。原因は初期化してなかったためです。下記のコマンドで初期化します。
sudo /usr/pgsql-12/bin/postgresql-12-setup initdb
再度確認します。今度はエラーがなく起動されていることが確認できます。
systemctl status postgresql-12
<出力結果>
● postgresql-12.service - PostgreSQL 12 database server
Loaded: loaded (/usr/lib/systemd/system/postgresql-12.service; enabled; vendor preset: disabled)
Active: active (running) since
Docs: https://www.postgresql.org/docs/12/static/
Process: 12818 ExecStartPre=/usr/pgsql-12/bin/postgresql-12-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
Main PID: 12824 (postmaster)
Tasks: 8 (limit: 11524)
Memory: 17.4M
CGroup: /system.slice/postgresql-12.service
tq12824 /usr/pgsql-12/bin/postmaster -D /var/lib/pgsql/12/data/
tq12825 postgres: logger
tq12827 postgres: checkpointer
tq12828 postgres: background writer
tq12829 postgres: walwriter
tq12830 postgres: autovacuum launcher
tq12831 postgres: stats collector
mq12832 postgres: logical replication launcher
ユーザー作成
postgresでログインして、ユーザーを作成します。
su - postgres
-bash-4.2$ createuser --pwprompt --interactive pgadmin
新しいロールのためのパスワード:
もう一度入力してください:
新しいロールをスーパユーザにしますか? (y/n)y
-bash-4.2$ exit
今度は、さきほど作成したユーザーでログインできるか確認してみます。
psql -h localhost -U pgadmin -d postgres
<出力結果>
psql: error: could not connect to server: FATAL: Ident authentication failed for user "pgadmin"
pg_hba.confを変更していなかったので、認証方式でエラーが発生しました。
認証方式を変更します。まずはバックアップを取ります。
## ログインしてなければ
su - postgres
## バックアップ
cp -piv ~/12/data/pg_hba.conf ~/12/data/pg_hba.conf.`date "+%Y%m%d"`
pg_hba.confの内の「ident → md5」 に変更します。
vi ~/12/data/pg_hba.conf
<編集>
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all peer
host replication all 127.0.0.1/32 md5
host replication all ::1/128 md5
再起動します。
sudo systemctl restart postgresql-12
ログインできるようになってことが確認できます。
psql -h localhost -U pgadmin -d postgres
Password for user pgadmin:
psql (12.2)
Type "help" for help.
postgres=# exit
postgresql.conf ・firewall設定
外部から接続する場合は、firewallを設定しておきます。
firewall-cmd --add-port=5432/tcp --zone=public --permanent
firewall-cmd --reload
postgresql.confも設定しておきます。
## ログインしてなければ
sudo su - postgres
## バックアップ
cp -piv ~/12/data/postgresql.conf ~/12/data/postgresql.conf.`date "+%Y%m%d"`
## postgresql.conf編集
vi ~/12/data/postgresql.conf
<編集箇所>
listen_addresses = '*'
port = 5432
pg_hba.confも編集します。ここではローカルネットワークからのみアクセスできるように編集してます。
vi ~/12/data/pg_hba.conf
<編集>
# IPv4 local connections:
host all all 127.0.0.1/32 md5
↓ 変更
host all all 192.168.0.0/16 md5
再起動して、設定を反映させておきます。
sudo systemctl restart postgresql-12
※CentOS8 pgAdmin4をインストールする手順はこちら
-
前の記事
windows10 fast.comのコマンドラインツール「fast-cli」を使って回線速度を計測する 2020.04.28
-
次の記事
Centos8 システム情報をコンソールに表示できる「screenFetch」をインストールする手順 2020.04.29
コメントを書く