LoginSignup
9
10

More than 1 year has passed since last update.

PostgreSQL 14.xをインストール(ソースからビルド)for RockyLinux 8.4

Last updated at Posted at 2016-09-19

概要

エンタープライズなDBで同じみのPostgreSQL。2021年10月時点では、14.0が最新版です。本環境は、RockyLinuxにソースからビルドする形での導入を行います。RHEL互換OS(CentOSやOracleLinuxなど)であれば同様の作業が可能です。
なお、あらかじめzlibが必要となります。

準備作業

OS標準のPostgreSQLが入っている場合、アンインストールする

rpm -e qt-postgresql-4.8.5-13.el7.x86_64
rpm -e qt5-qtbase-postgresql-5.6.1-10.el7.x86_64
rpm -e postgresql-devel-9.2.18-1.el7.x86_64
rpm -e qt3-PostgreSQL-3.3.8b-51.el7.x86_64
rpm -e postgresql-libs-9.2.18-1.el7.x86_64

あらかじめ、ユーザーと作業ディレクトリを作成する

準備
mkdir /usr/local/pgsql 
useradd postgres
chown postgres:postgres /usr/local/pgsql 

検証環境バージョン

ソフトウェア バージョン
PostgreSQL 14.1
OS RockeyLiux 8.5

インストール作業

インストール作業
cd /usr/local/src
wget https://ftp.postgresql.org/pub/source/v14.1/postgresql-14.1.tar.gz
tar xvzf postgresql-14.1.tar.gz
chown -R postgres:postgres postgresql-14.1
su postgres
cd postgresql-14.1
./configure --without-readline
make
make install
環境変数を設定する
su postgres
vi ~/.bashrc
環境変数
#以下の内容を最下部に書いて保存
export PATH="$PATH":/usr/local/pgsql/bin 
export POSTGRES_HOME=/usr/local/pgsql 
export PGLIB=$POSTGRES_HOME/lib 
export PGDATA=$POSTGRES_HOME/data 
export MANPATH="$MANPATH":$POSTGRES_HOME/man 
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH":"$PGLIB"  
環境変数を反映
source ~/.bashrc

初期設定

DBを初期化する

初期化
initdb --no-locale 

Listenアドレスの設定

通常はローカルホストのみの設定となるが、クラサバ型で他のAPサーバーやクライアントからDBにアクセスされる場合は、適宜アクセスできる範囲を広げる。
今回は、ANYアドレスから通信を受けるように設定(セキュリティ的に注意が必要)

Listenアドレスの設定
vi /usr/local/pgsql/data/postgresql.conf
ListenIPの指定
#listen_addresses = 'localhost'
↓
listen_addresses = '*'
ListenIP範囲の設定
vi $PGDATA/pg_hba.conf
hba_confの設定
host    all             all              0.0.0.0/0               trust

起動を確認

起動する
pg_ctl -w start

管理ユーザーを追加する

createuser admin -s -P

一度PostgreSQLを停止する

pg_ctl -w stop

起動スクリプト

起動スクリプトは、CentOS7.2で提供されるrpmより構成します。

起動スクリプトを作成
su root

cat > /usr/lib/systemd/system/postgresql.service << EOF
# 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.service",
# containing
#   .include /lib/systemd/system/postgresql.service
#   ...make your changes here...
# For more info about custom unit files, see
# http://fedoraproject.org/wiki/Systemd#How_do_I_customize_a_unit_file.2F_add_a_custom_unit_file.3F

# For example, if you want to change the server's port number to 5433,
# create a file named "/etc/systemd/system/postgresql.service" containing:
#   .include /lib/systemd/system/postgresql.service
#   [Service]
#   Environment=PGPORT=5433
# This will override the setting appearing below.

# Note: changing PGPORT or PGDATA will typically require adjusting SELinux
# configuration as well; see /usr/share/doc/postgresql-*/README.rpm-dist.

# Note: do not use a PGDATA pathname containing spaces, or you will
# break postgresql-setup.

# Note: in F-17 and beyond, /usr/lib/... is recommended in the .include line
# though /lib/... will still work.

[Unit]
Description=PostgreSQL database server
After=network.target

[Service]
Type=forking

User=postgres
Group=postgres

# Port number for server to listen on
Environment=PGPORT=5432

# Location of database directory
Environment=PGDATA=/usr/local/pgsql/data

# Where to send early-startup messages from the server (before the logging
# options of postgresql.conf take effect)
# This is normally controlled by the global default set by systemd
# StandardOutput=syslog

# Disable OOM kill on the postmaster
OOMScoreAdjust=-1000

#ExecStartPre=/usr/local/pgsql/bin/postgresql-check-db-dir \${PGDATA} <=このモジュールは存在しないのでコメント
ExecStart=/usr/local/pgsql/bin/pg_ctl start -D \${PGDATA} -s -o "-p \${PGPORT}" -w -t 300
ExecStop=/usr/local/pgsql/bin/pg_ctl stop -D \${PGDATA} -s -m fast
ExecReload=/usr/local/pgsql/bin/pg_ctl reload -D \${PGDATA} -s

# Give a reasonable amount of time for the server to start up/shut down
TimeoutSec=300

[Install]
WantedBy=multi-user.target

EOF

サービスでの起動と停止

サービス起動
systemctl start postgresql
サービス停止
systemctl stop postgresql
自動起動
systemctl enable postgresql
9
10
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
9
10