IT技術で仕事を減らしたい!

ITエンジニアのメモ+α

CentOS7にPostgreSQL10サーバを構築

どうも、nippaです。

今回はPostgreSQL10サーバの構築です。

データ蓄積用のRDB(Relational database)を利用したく、構築してみます。

インストール手順は公式に沿っています。

環境

Virtual Box
OS: CentOS7
DB: PostgreSQL10

1. レポジトリの登録

CentOS7では”yum"で簡単にレポジトリを登録することができます。

yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

2. PostgreSQL10のダウンロード

クライアントパッケージとサーバパッケージをダウンロードします。

# クライアントパッケージ
yum install postgresql10

# サーバパッケージ
yum install postgresql10-server

3. postgresユーザの作成

パッケージインストールの場合、postgresユーザが自動に生成されています。

# ユーザの確認
cat /etc/passwd | grep postgres

# 結果
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash

作成されていない場合は、以下のコマンドで作成する。

# postgres ユーザの追加
useradd postgres --create-home /var/lib/pgsql -s /bin/bash -c "PostgreSQL Server"

# ユーザの確認
cat /etc/passwd | grep postgres

4. PostgreSQL10のインストール

PostgreSQLのデータベースファイルの保存先のディレクトリを指定して、インストールを行います。ここでは保存先を/work/pgsql10/dataとします。

# rootユーザからpostgresユーザにスイッチ
su - postgres

# Pathの設定
echo "PATH=/usr/pgsql-10/bin:$PATH" >> ~/.bash_profile

インストール先を設定します。

 vim ~/.bash_profile
PGDATA=/work/pgsql10/data
export PGDATA

PostgreSQL10のインストールします。

PGSETUP_INITDB_OPTIONS="-D /work/pgsql10/data -E utf-8" postgresql-10-setup initdb

インストール状況の確認します。

ls /work/pgsql10/data
base          pg_ident.conf  pg_serial     pg_tblspc    postgresql.auto.conf
global        pg_logical     pg_snapshots  pg_twophase  postgresql.conf
pg_commit_ts  pg_multixact   pg_stat       PG_VERSION
pg_dynshmem   pg_notify      pg_stat_tmp   pg_wal
pg_hba.conf   pg_replslot    pg_subtrans   pg_xact

4. PostgreSQLの起動

/usr/lib/systemd/system/postgresql-10.serviceのファイル内のPGDATA

Environment=PGDATA=/work/pgsql10/data/

に変更しあmす。 PostgreSQLランレベルを設定して、起動させます。

# ランレベル設定
systemctl enable postgresql-10

# 起動
systemctl start postgresql-10

# 停止
systemctl stop postgresql-10

5. データベースのアクセス

初期設定状態でデータベースにアクセスできるかを確認します。 postgresユーザに変更して、データベースへのログインできれば完了です。

# postgresユーザに変更
su - postgres

# データベースへのログイン
psql -h localhost -p 5432 -U postgres -d postgres

感想

PostgreSQL12 を以前構築しましたが、今回Postgres10までしか対応していないソフトウェアをインストールするために、再度構築してみました。

ではでは、また次回。