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

ITエンジニアのメモ+α

PostgreSQL12サーバの構築

どうも、nippaです。

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

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

環境

Virtual Box
OS: CentOS8
DB: PostgreSQL12

1. レポジトリの登録

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

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

ビルドされているPostgreSQLを無効化します。

dnf -qy module disable postgresql

2. PostgreSQLのダウンロード

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

# クライアントパッケージ
dnf install postgresql12

# サーバパッケージ
dnf install postgresql12-server

3. PostgreSQLのインストール

PostgreSQLのデータベースファイルの保存先のディレクトリを指定する。 PostgreSQLサービスの設定ファイル/usr/lib/systemd/system/postgresql-12.serviceを編集して、保存先を指定する。

# Defaults
Environment=PGDATA=/var/lib/pgsql/12/data

# ここでは適当に以下にインストールします。
Environment=PGDATA=/work/pgsql12/data

データベースの初期化を行います。

/usr/pgsql-12/bin/postgresql-12-setup initdb

を実行し、環境変数”Environment”で指定した先に、

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

ファイルができていればインストール完了です。

また、自動でpostgresユーザが作成されます。念の為確認しておきます。

cat /etc/passwd 

# postgres ユーザ
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash

4. PostgreSQLの起動

PostgreSQLランレベルを設定して、起動させます。

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

# 起動
systemctl start postgresql-12

# 停止
systemctl stop postgresql-12

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

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

# postgresユーザに変更
su - postgres

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

感想

以前、構築した際と比較して、かなり簡単になっていました。

基本的には、コマンドを打つだけで構築できてしますので、特に考えることもないです。

時間があれば、Postgreサーバの設定もやっておきたいところです。

ではでは、また次回。