LoginSignup
3
4

More than 1 year has passed since last update.

CentOS 8にPostgreSQL 13をインストールする(サーバー、クライアントのみそれぞれ)

Last updated at Posted at 2021-07-17

What's?

タイトル通り。CentOS 8に、PostgreSQL 13をインストールしてみたいと思います。

PostgreSQLをインストールして終了、もなんなので、クライアント(主にpsql)のみのインストールもやりたいと思います。

環境

今回の環境は、こちらです。

$ cat /etc/redhat-release
CentOS Linux release 8.4.2105


$ uname -srvmpio
Linux 4.18.0-305.3.1.el8.x86_64 #1 SMP Tue Jun 1 16:14:33 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

インストール方法を確認する

インストール方法は、PostgreSQLのダウンロードページを見るのがよいでしょう。

この中から、「Linux」→「Red Hat/Rocky/CentOS」を選択します。

image.png

ちなみに、RPMから探すならここになるみたいです。

PostgreSQLをインストールする

それでは、PostgreSQLをインストールしましょう。

yumリポジトリのインストール。

$ sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm

CentOS標準で使えてPostgreSQLの無効化。

$ sudo dnf -qy module disable postgresql

PostgreSQLサーバーのインストール。

$ sudo dnf install -y postgresql13-server

今回は、PostgreSQL 13.3がインストールされました。

$ psql --version
psql (PostgreSQL) 13.3

ちなみに、今回インストールしたPostgreSQLのyumリポジトリは、PostgreSQL 9.6以降が扱えるようです。

$ dnf search postgresql*-server
メタデータの期限切れの最終確認: 0:02:27 時間前の 2021年07月17日 19時01分32秒 に実施しました。
======================================================================= 名前 一致: postgresql*-server =======================================================================
postgresql10-server.x86_64 : The programs needed to create and run a PostgreSQL server
postgresql11-server.x86_64 : The programs needed to create and run a PostgreSQL server
postgresql12-server.x86_64 : The programs needed to create and run a PostgreSQL server
postgresql13-server.x86_64 : The programs needed to create and run a PostgreSQL server
postgresql96-server.x86_64 : The programs needed to create and run a PostgreSQL server

以前のバージョンを指定してインストールすることも可能ですね。

$ sudo dnf install postgresql12-server-12.3

少し話が逸れました。続きをやりましょう。データベースの初期化を行います。

$ sudo PGSETUP_INITDB_OPTIONS='--encoding=UTF-8 --no-locale' /usr/pgsql-13/bin/postgresql-13-setup initdb

PGSETUP_INITDB_OPTIONS環境変数を使い、エンコーディングはUTF-8に、ロケールはなしにしました。

systemdのサービスとして有効化して

$ sudo systemctl enable postgresql-13

起動。

$ sudo systemctl start postgresql-13

各種ファイルなどは、/var/lib/pgsql/13ディレクトリにあります。

[vagrant@centos8 /]$ sudo ls -l /var/lib/pgsql/13
total 8
drwx------.  2 postgres postgres    6  5月 18 22:24 backups
drwx------. 20 postgres postgres 4096  7月 17 19:09 data
-rw-------.  1 postgres postgres  918  7月 17 19:07 initdb.log

接続確認しましょう。

postgresユーザーに変更。

$ sudo su - postgres

psqlで確認。

$ psql
psql (13.3)
Type "help" for help.

postgres=# select version();
                                                version                                                 
--------------------------------------------------------------------------------------------------------
 PostgreSQL 13.3 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 8.4.1 20200928 (Red Hat 8.4.1-1), 64-bit
(1 row)

ここで、ユーザーとデータベースを作成します。

# create user myuser password 'password';
CREATE ROLE


# create database example owner myuser;
CREATE DATABASE

このユーザーは、この後インストールする、別のサーバーからログインして確認するようにしましょう。

/var/lib/pgsql/13/data/postgresql.confファイルのlisten_addressesを修正。

/var/lib/pgsql/13/data/postgresql.conf
listen_addresses = '*'                  # what IP address(es) to listen on;
                                        # comma-separated list of addresses;
                                        # defaults to 'localhost'; use '*' for all
                                        # (change requires restart)

/var/lib/pgsql/13/data/pg_hba.confファイルの末尾に以下を追加。

/var/lib/pgsql/13/data/pg_hba.conf
host    all             all             192.168.33.1/24        scram-sha-256

sudoが使えるユーザーに変更後、PostgreSQLを再起動。

$ sudo systemctl restart postgresql-13

接続確認。なお、このサーバーのIPアドレスは192.168.33.12とします。

$ psql -U myuser -h 192.168.33.12 example

OKですね。

ちなみに、pg_hba.confファイルの初期値は以下だったので

$ sudo grep -vE '#|^$' /var/lib/pgsql/13/data/pg_hba.conf
local   all             all                                     peer
host    all             all             127.0.0.1/32            scram-sha-256
host    all             all             ::1/128                 scram-sha-256
local   replication     all                                     peer
host    replication     all             127.0.0.1/32            scram-sha-256
host    replication     all             ::1/128                 scram-sha-256

特に変更せずとも、127.0.0.1での指定なら接続可能です。

$ psql -U myuser -h 127.0.0.1 example

firewalldの設定を変更する

ここで、firewalldの状態を見てみます。

$ sudo firewall-cmd --get-active-zones
public
  interfaces: eth0 eth1

動作している場合は、PostgreSQLへの通信が可能なのか確認しておきましょう

$ sudo firewall-cmd --list-services --zone=public
cockpit dhcpv6-client ssh

今回は不足しているので、追加しておきます。

$ sudo firewall-cmd --add-service=postgresql --permanent --zone=public
success

確認。

$ sudo firewall-cmd --list-services --zone=public
cockpit dhcpv6-client postgresql ssh

PostgreSQLのクライアントのみをインストールする

続いて、他のサーバーにPostgreSQLのクライアントのみをインストールしてみます。

yumリポジトリのインストールと、OS標準のPostgreSQLの無効化までは同じです。

$ sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm
$ sudo dnf -qy module disable postgresql

クライアントパッケージは、postgresql13のようです。

$ sudo dnf search postgresql13
メタデータの期限切れの最終確認: 0:03:32 時間前の 2021年07月17日 19時56分19秒 に実施しました。
======================================================================== 名前 完全一致: postgresql13 ========================================================================
postgresql13.x86_64 : PostgreSQL client programs and libraries
========================================================================== 名前 一致: postgresql13 ==========================================================================
postgresql13-contrib.x86_64 : Contributed source and binaries distributed with PostgreSQL
postgresql13-devel.x86_64 : PostgreSQL development header files and libraries
postgresql13-docs.x86_64 : Extra documentation for PostgreSQL
postgresql13-libs.x86_64 : The shared libraries required for any PostgreSQL clients
postgresql13-llvmjit.x86_64 : Just-in-time compilation support for PostgreSQL
postgresql13-odbc.x86_64 : PostgreSQL ODBC driver
postgresql13-plperl.x86_64 : The Perl procedural language for PostgreSQL
postgresql13-plpython3.x86_64 : The Python3 procedural language for PostgreSQL
postgresql13-pltcl.x86_64 : The Tcl procedural language for PostgreSQL
postgresql13-server.x86_64 : The programs needed to create and run a PostgreSQL server
postgresql13-test.x86_64 : The test suite distributed with PostgreSQL

というわけで、postgresql13をインストール。

$ sudo dnf install postgresql13

これでインストールされるのは、これくらいのコマンドですね。

$ rpm -ql postgresql13 | grep /bin/
/usr/pgsql-13/bin/clusterdb
/usr/pgsql-13/bin/createdb
/usr/pgsql-13/bin/createuser
/usr/pgsql-13/bin/dropdb
/usr/pgsql-13/bin/dropuser
/usr/pgsql-13/bin/pg_basebackup
/usr/pgsql-13/bin/pg_config
/usr/pgsql-13/bin/pg_dump
/usr/pgsql-13/bin/pg_dumpall
/usr/pgsql-13/bin/pg_isready
/usr/pgsql-13/bin/pg_receivewal
/usr/pgsql-13/bin/pg_restore
/usr/pgsql-13/bin/pg_waldump
/usr/pgsql-13/bin/pgbench
/usr/pgsql-13/bin/psql
/usr/pgsql-13/bin/reindexdb
/usr/pgsql-13/bin/vacuumdb

接続確認。

$ psql -U myuser -h 192.168.33.12 -W example
パスワード: 
psql (13.3)
"help"でヘルプを表示します。

example=> select version();
                                                version                                                 
--------------------------------------------------------------------------------------------------------
 PostgreSQL 13.3 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 8.4.1 20200928 (Red Hat 8.4.1-1), 64-bit
(1 行)

OKですね。

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4