postgreSQL サーバーのインストールとセットアップ

はじめに

postgreSQLサーバーのインストールとセットアップについて、実際にやったこととその記録です。

環境

$ cat /etc/os-release
NAME="GalliumOS"
VERSION="3.1 (Bismuth)"
ID=galliumos
ID_LIKE="ubuntu debian"
PRETTY_NAME="GalliumOS 3.1"
VERSION_ID="3.1"
  :
VERSION_CODENAME=bismuth
UBUNTU_CODENAME=bionic

インストール

バイナリ or ソースコードビルドのどちらかを実行します。

バイナリから

PostgreSQL情報ポータルサイトに従い、Synapticパッケージマネージャを利用しました。
以下の7つのパッケージをインストールするようです。少しバージョンが古いようですね。

  • libpq5
  • postgresql
  • postgresql-10
  • postgresql-client-10
  • postgresql-client-common
  • postgresql-common
  • sysstat

Synapticがなくても、これらをapt installすれば同じ状態になると思います。
アンインストールは上記のパッケージのうち、postgresql から始まるもの5つを$ sudo apt remove --purge postgresql... として順番に削除していき、PCを再起動してからsudo userdel postgresを実行します。アンインストール参考サイト

< その他インストール参考サイト>
- Ubuntu documentation / PostgreSQL

ソースコードからビルド

PostgreSQL文書の方法に基づいて、ver13.3をインストールします。

まず、こちらのサイトよりソースコードをダウンロードします。

$ cd ~/Downloads
$ echo -n "0b54a8a68dbfaf5dcddd89eb3922740143df50fbea02fefda8de743d8af99516  postgresql-13.3.tar.gz" | sha256sum -c
# OKがでればダウンロードデータは正しいデータである

ダウンロードしたデータを展開します。

$ mv ~/Downloads/postgresql-13.3.tar.gz ~/
$ cd ~
$ tar zxvf postgresql-13.3.tar.gz

以下のコマンドを実行してインストールします。

$ cd ~/postgresql-13.3

# 構成
# オプション設定は任意です
# デフォルトのインストール位置は `/usr/local/pgsql`です
$ ./configure --with-openssl


# 構築
$ make
# "All of PostgreSQL successfully made. Ready to install."
# が表示されれば成功
# 私は15分間くらいかかりました
 
# リグレッションテスト
$ make check

# install
$ sudo make install
$ sudo make install-docs

セットアップ

ここからはセットアップです。
以下はソースコードからのビルド後のセットアップです。

# postgresユーザー作成
# グループ名もpostgresです
$ sudo groupadd postgres
$ sudo useradd -g postgres -m postgres
$ sudo passwd postgres
# パスワードを決めて入力する
# postgresユーザーのデフォルトシェルをbashに設定する
$ sudo chsh -s /bin/bash postgres

# 設定ディレクトリのオーナーの変更
$ sudo chown -R postgres:postgres /usr/local/pgsql

# ログファイルの作成
$ sudo touch /var/log/postgresql.log
$ sudo chown postgres:postgres /var/log/postgresql.log
$ sudo chmod 644 /var/log/postgresql.log

# ユーザーの変更
$ su - postgres
# postgresユーザーのパスワードを入力する
# これ以降のコマンドはpostgresユーザーで実行する必要があります

# 環境変数設定
# 以下(5行)を~postgres/.bash_profile に追加
$ vi ~/.bash_profile
export PGHOME=/usr/local/pgsql
export PGDATA=$PGDATA/data`
export PATH=$PATH:$HOME/bin:$PGHOME/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$PGHOME/lib
. ~/.bashrc

# 環境変数設定の反映
$ . ~/.bash_profile

# データベースクラスタの作成
# --no-locale オプションは日本語表示を正しく行うため
$ mkdir -p $PGDATA
$ initdb --no-locale

# DBサーバーの起動
# $PGDATAを設定しておくことにより、Dオプションの設定が不要となる
$ pg_ctl -l /var/log/postgresql.log start

# データベースの新規作成
$ createdb test

# データベース名の変更
$ psql -c "ALTER DATABASE old_name RENAME TO new_name"

# データベースへのログイン
$ psql test

# DBサーバーの終了
$ pg_ctl stop

# postgresユーザーを終了する
$ exit

次回以降PC起動時の操作

非postgresユーザーであるとします。

# DBサーバー起動
$ su - postgres -c 'pg_ctl -l /var/log/postgresql.log start'
# postgresユーザーのパスワードを入力

# DBサーバー停止
$ su - postgres -c 'pg_ctl stop'
# postgresユーザーのパスワードを入力

# DBサーバー動作状態の確認
$ su - postgres -c 'pg_ctl status'
# postgresユーザーのパスワードを入力

参考サイトなど

< セットアップ部の参考サイト>

上記2つのサイトにかかれているchkconfigコマンドは現在(2021年8月)はもう使えないようなので(Ubuntu で chkconfig の代わりに update-rc.d を使う)、こちらのコメントに従って、su - postgres -c 'pg_ctl' で制御することにします。postgresユーザーとしてDBサーバーの起動・停止を行う必要があるので、postgresユーザーのパスワードを入力する必要があります。

< 監視項目部の参考サイト >

その他

ソースビルド時のpostgresのアンインストール

Makefileがあるディレクトリにて、sudo make uninstallを行う。

# DBサーバーの停止
$ su - postgres -c 'pg_ctl stop'

$ cd ~/postgresql-13.3
$ sudo make uninstall

# データディレクトリを削除
$ sudo rm -rf /usr/local/pgsql

# ログファイルの削除
$ sudo rm /var/log/postgresql.log

# postgres ユーザーの削除
$ sudo userdel -r postgres

< 参考サイト >

つまづきポイント1

postgresユーザーのホームディレクトリを作成せずに、ユーザー作成してしまった。postgresユーザーのホームディレクトリは必要。~/.bash_profileなどを登録するため。
ユーザー作成後にホームディレクトリを作成する方法はmkhomedir_helper関数を用いる。参考サイト
上記セットアップコマンドでは useraddコマンドの-mオプションにてホームディレクトリを作成するようにしている。

$ sudo mkhomedir_helper postgres

つまづきポイント2

postgresユーザーの元々のシェルがbashになっていなかった。(元々/bin/shになってた)
なので、postgresユーザーになったときに、~/.bash_profileに記載した環境変数の設定が自動で読み込まれず、DBサーバーが起動しなかった。
ユーザーのデフォルトシェルの変更はchshコマンドで可能。
新規ユーザー作成時のシェル設定は useradd -D で確認・設定可能。

# 現在の設定状態を表示する
$ sudo useradd -D

# シェルの変更
$ sudo useradd -D -s /bin/bash

2022.03.06追記

ubuntu20.04上にてapt コマンドによるインストールを行うときは以下となる。

Ubuntu documentation / PostgreSQL

# apt コマンドによるインストール
$ sudo apt install postgresql postgresql-contrib
# start コマンド
$ pg_ctlcluster 12 main start
# stop コマンド
$ pg_ctlcluster 12 main stop
# もしくは
# systemctl によるstart / stop / status
$ sudo systemctl start postgresql@12-main
$ sudo systemctl stop postgresql@12-main
$ sudo systemctl status postgresql@12-main

systemctl statusのメッセージで、起動時のコマンドや設定ファイルがわかります。

またsystemctl startを実行して起動できた時点で、postgresのinitdbコマンドで実行するデータベースクラスタの作成はすでに完了しているようです。

elixirのwebフレームワークであるphoenixのセットアップのmix ecto.setpにて以下のエラーが発生する場合は、
postgresqlでのpostgresユーザーのパスワードをpostgresに設定します。

# エラー内容
$ mix ecto.setup
23:18:07.344 [error] GenServer #PID<0.290.0> terminating
** (Postgrex.Error) FATAL 28P01 (invalid_password) password authentication failed for user "postgres"
    (db_connection 2.4.2) lib/db_connection/connection.ex:100: DBConnection.Connection.connect/2
    (connection 1.1.0) lib/connection.ex:622: Connection.enter_connect/5
    (stdlib 3.17) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
Last message: nil
State: Postgrex.Protocol

23:18:07.352 [error] GenServer #PID<0.297.0> terminating
** (Postgrex.Error) FATAL 28P01 (invalid_password) password authentication failed for user "postgres"
    (db_connection 2.4.2) lib/db_connection/connection.ex:100: DBConnection.Connection.connect/2
    (connection 1.1.0) lib/connection.ex:622: Connection.enter_connect/5
    (stdlib 3.17) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
Last message: nil
State: Postgrex.Protocol
** (Mix) The database for Realworld.Repo couldn't be created: killed
$ sudo -u postgres psql postgres
postgres=# \password postgres
(パスワードを2回入力)
postgres=# \q
$ mix ecto.setup
The database for *** has been created
00:00:00.000 [info]  Migrantions already up
$

以上です。