Yumを使用して最新のPostgreSQLをOracleLinuxにインストールする。
またPostgreSQLクラスタの場所はデフォルトから変更する。
環境
-
Oracle Linux Server release 7.6
-
PostgreSQL 11
Yumリポジトリ登録
以下サイトで「PostgreSQL Yum Repository」にて以下を入力するとリポジトリ登録のYumコマンドが生成されるのでそれを実行。
Linux downloads (Red Hat family)
https://www.postgresql.org/download/linux/redhat/
-
Select version:(11)
-
Select platform:(RedHat Enterprise,CentOS,Scientific or Oracle Linux 7)
-
Select architecture:(X86_64)
もしくは以下から、「Oracle Enterprise Linux 7 - x86_64」からリポジトリURLをコピペでもOK。
PostgreSQL RPM Building Project - Repository Packages
https://yum.postgresql.org/repopackages.php
# yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
<中略>
Installed:
pgdg-redhat-repo.noarch 0:42.0-4
Complete!
インストール
postgresql11では以下のパッケージが含まれる。
# yum search postgresql11
Loaded plugins: ulninfo
=========================== N/S matched: postgresql11 ============================
postgresql11-debuginfo.x86_64 : Debug information for package postgresql11
postgresql11.x86_64 : PostgreSQL client programs and libraries
postgresql11-contrib.x86_64 : Contributed source and binaries distributed with PostgreSQL
postgresql11-devel.x86_64 : PostgreSQL development header files and libraries
postgresql11-docs.x86_64 : Extra documentation for PostgreSQL
postgresql11-libs.x86_64 : The shared libraries required for any PostgreSQL clients
postgresql11-llvmjit.x86_64 : Just-in-time compilation support for PostgreSQL
postgresql11-odbc.x86_64 : PostgreSQL ODBC driver
postgresql11-plperl.x86_64 : The Perl procedural language for PostgreSQL
postgresql11-plpython.x86_64 : The Python procedural language for PostgreSQL
postgresql11-pltcl.x86_64 : The Tcl procedural language for PostgreSQL
postgresql11-server.x86_64 : The programs needed to create and run a PostgreSQL server
postgresql11-tcl.x86_64 : A Tcl client library for PostgreSQL
postgresql11-test.x86_64 : The test suite distributed with PostgreSQL
Name and summary matches only, use "search all" for everything.
ここではPostgreSQL をサーバ(postgresql11-server)をインストールする。
# yum install postgresql11-server
以下にインストールされる。
# ls -d /usr/pgsql-11
/usr/pgsql-11
# ls /usr/pgsql-11
bin lib share
postgresユーザが作成される。
# id postgres
uid=26(postgres) gid=26(postgres) groups=26(postgres)
PostgreSQLクラスタの作成(initdb)
postgresql-11-setup initdb
を使用してPostgreSQLクラスタを初期化する。
環境変数:PGSETUP_INITDB_OPTIONSでinitdbのオプションを指定できる。
# grep -A 4 PGSETUP_INITDB_OPTIONS /usr/pgsql-11/bin/postgresql-11-setup
PGSETUP_INITDB_OPTIONS Options carried by this variable are passed to
subsequent call of \`initdb\` binary (see man
initdb(1)). This variable is used also during
'upgrade' mode because the new cluster is actually
re-initialized from the old one.
<以下略>
ここではエンコーディング(-E UTF8)、ロケール(--locale=C)、クラスタの場所(--pgdata /pgdata)を指定する。
postgresql-11-setup initdb
実行時に環境変数を指定する。なお、/pgdataディレクトリは所有者postgres:postgresで作成済みとする。
# PGSETUP_INITDB_OPTIONS="-E UTF8 --locale=C --pgdata /pgdata" /usr/pgsql-11/bin/postgresql-11-setup initdb
PostgreSQLの起動前の設定
systemctl start postgresql-11.service
でPostgreSQLのサービスを起動する。
起動する前にPostgreSQLのクラスタの場所を変更しているため設定を見直す。
/usr/lib/systemd/system/postgresql-11.service
の冒頭部分を確認すると、
# It's not recommended to modify this file in-place, because it will be
# overwritten during package upgrades. If you want to customize, the
# best way is to create a file "/etc/systemd/system/postgresql-11.service",
# containing
# .include /usr/lib/systemd/system/postgresql-11.service
# ...make your changes here...
とあるので、/etc/systemd/system/postgresql-11.service
を作成し、以下内容で編集する。
- 「/usr/lib/systemd/system/postgresql-11.service」のinclude
- 「Environment=PGDATA=/pgdata」の設定
# vi /etc/systemd/system/postgresql-11.service
# cat /etc/systemd/system/postgresql-11.service
.include /usr/lib/systemd/system/postgresql-11.service
[Service]
Environment=PGDATA=/pgdata
PostgreSQLの起動
設定をリロードし、起動する。
# systemctl daemon-reload
# systemctl start postgresql-11.service
# systemctl status postgresql-11.service
● postgresql-11.service - PostgreSQL 11 database server
Loaded: loaded (/usr/lib/systemd/system/postgresql-11.service; disabled; vendor preset: disabled)
Active: active (running) since Sun 2019-07-07 03:40:47 UTC; 3s ago
Docs: https://www.postgresql.org/docs/11/static/
Process: 16333 ExecStartPre=/usr/pgsql-11/bin/postgresql-11-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
Main PID: 16338 (postmaster)
CGroup: /system.slice/postgresql-11.service
tq16338 /usr/pgsql-11/bin/postmaster -D /pgdata
tq16342 postgres: logger
tq16344 postgres: checkpointer
tq16345 postgres: background writer
tq16346 postgres: walwriter
tq16347 postgres: autovacuum launcher
tq16348 postgres: stats collector
mq16349 postgres: logical replication launcher
自動起動を設定する場合は、以下を実行する。
# systemctl enable postgresql-11
Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql-11.service to /usr/lib/systemd/system/postgresql-11.service.
psqlによる接続確認
接続して、バージョンとデータベースを確認する。
# su - postgres
-bash-4.2$ export PGDATA=/pgdata
-bash-4.2$ export PATH=/usr/pgsql-11/bin:$PATH
-bash-4.2$ psql
psql (11.4)
Type "help" for help.
postgres=# SELECT version();
version
---------------------------------------------------------------------------------------------------------
PostgreSQL 11.4 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36), 64-bit
(1 row)
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+---------+-------+-----------------------
postgres | postgres | UTF8 | C | C |
template0 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)
postgres=#
postgres=# \q
参考資料
-
How to install PostgreSQL 9.x on Oracle Linux 7.x
-
Linux downloads (Red Hat family)
-
PostgreSQL RPM Building Project - Repository Packages
-
How to Install PostgreSQL 11 on CentOS/RHEL 7/6
-
PostgreSQL 11.3文書 initdb
-
CentOS7で、PostgreSQLのデータディレクトリを変更する