アプリケーション開発ポータルサイト
ServerNote.NET
Amazon.co.jpでPC関連商品タイムセール開催中!
カテゴリー【PostgreSQLUbuntuDebian
PostgreSQLをソースコードからインストールしてsystemdのサービスとして動かす
POSTED BY
2023-06-08

Ubuntu/Debianでは/etc/init.d + chkconfigではなく、/lib/systemd/system + systemctlの方式を取る。

aptでパッケージインストールしたサーバーソフトならそのまま自動でsystemdに登録されるので問題ないが、ソースコードからコンパイルしてインストールしたサーバーソフトは、手動でサービスファイルを作成しsystemdに登録して起動・停止を行う必要がある。

https://www.postgresql.jp/document/9.6/html/server-start.html

を参考にして、Ubuntu ServerにPostgreSQL14.3をソースからインストール・systemdに登録・起動・停止・自動起動まで行う。

/usr/local/pgsqlにインストール

--with-systemdをつけるのが最大のポイント。

sudo -s
apt install build-essential libreadline-dev zlib1g-dev  libsystemd-dev
cd /usr/local/src
wget https://ftp.postgresql.org/pub/source/v14.3/postgresql-14.3.tar.gz
tar xvfzp postgresql-14.3.tar.gz
cd postgresql-14.3
./configure --prefix=/usr/local/pgsql --with-systemd
make
make install

postgresユーザ・グループ・データ初期化

sudo -s
groupadd postgres
useradd -d /usr/local/pgsql -g postgres postgres
chown -Rh postgres:postgres /usr/local/pgsql
sudo -s -u postgres
cd /usr/local/pgsql
mkdir data
chmod go-rwx data
./bin/initdb –no-locale -D /usr/local/pgsql/data

/lib/systemd/system/postgresql.serviceの作成

以下の内容で作成する。

[Unit]
Description=PostgreSQL database server
Documentation=man:postgres(1)

[Service]
Type=notify
User=postgres
ExecStart=/usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data
ExecReload=/bin/kill -HUP $MAINPID
KillMode=mixed
KillSignal=SIGINT
TimeoutSec=0

[Install]
WantedBy=multi-user.target

systemctlによる起動・終了・自動起動登録

ここまでくれば、あとはsystemctlが認識する。

確認

systemctl status postgresql

起動・確認

systemctl start postgresql
systemctl status postgresql

終了・確認

systemctl stop postgresql
systemctl status postgresql

自動起動登録・確認

systemctl enable postgresql
systemctl status postgresql

これでマシン起動時に自動的にpostgresqlが立ち上がり、終了時自動でstopしてくれる。

自動起動登録をやめるにはsystemctl disable postgresqlとすればよい。

※本記事は当サイト管理人の個人的な備忘録です。本記事の参照又は付随ソースコード利用後にいかなる損害が発生しても当サイト及び管理人は一切責任を負いません。
※本記事内容の無断転載を禁じます。
【WEBMASTER/管理人】
自営業プログラマーです。お仕事ください!
ご連絡は以下アドレスまでお願いします★

☆ServerNote.NETショッピング↓
ShoppingNote / Amazon.co.jp
☆お仲間ブログ↓
一人社長の不動産業務日誌
【キーワード検索】