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

ITエンジニアのメモ+α

Ubuntu 18.04にPostgreSQL10をインストール

どうも、nippaです。

またもやPostgreSQLのインストールです。

UbuntuPostgreSQLの設定を調べたのでまとめておきます。

環境

AWS EC2インスタンス
OS: Ubuntu 18.04

1. apt-cacheでパッケージの確認、apt-getでインストール

まず、パッケージの確認をします。

apt-cache show postgresql

Package: postgresql postgresql-common
Architecture: all
Version: 10+190ubuntu0.1
....

確認したパッケージをインストールする。

apt-get install postgresql

UbuntuではパッケージでPostgreSQLをインストールすると、自動的にsystemdに登録され、サービスが実行されます。

2. データ保存先の変更

デフォルトのデータ保存先は上で起動の際に指定した、

/var/lib/postgresql/10/main

になります。また、PostgreSQLの設定ファイルは、

ls /etc/postgresql/10/main/
conf.d  environment  pg_ctl.conf  pg_hba.conf  pg_ident.conf  postgresql.conf  start.conf

にあります。今回は、/work/postgresql/10/mainを保存ディレクトリに指定します。

systemdでサービスが起動している場合には、

systemctl stop postgresql

で停止します。

/etc/postgresql/10/main/postgresql.confをエディタで開き、data_directoryを以下のように書き換えます。

data_directory = '/work/postgresql/10/main'

rootユーザで、元のデータ保存ディレクトリを移動させます。

# ディレクトリを作成
mkdir -p /work/postgresql/10/main

# ディレクトリへ移動
cd /work

# ディレクトリの所有者をpostgresに変更
chown -R postgres:postgres postgresql

# 所有者の確認
ls -l

# 元のデータ保存ディレクトリを新たな保存ディレクトリに同期
rsync -av /var/lib/postgresql/10/main /work/postgresql/10

同期が終了したら、デーモンをリロードさせます。ディレクトリを変更したため、必須になります。

systemctl daemon-reload

サービスをスタートさせます。

systemctl start postgresql

systemctl status postgresql

PostgreSQLサーバを起動させます。

postgresユーザでPostgreSQLにログインして、データ保存ディレクトリを確認します。

su - postgres

psql

SQL

SHOW data_directory;

# 指定したディレクトリになっているかを確認
/work/postgresql/10/main

以上で、完了です。

感想

CentOSでこれまでやってきましたが、Ubuntu サーバで構築が必要になったため、まとめてみました。

UbuntuCentOSと比較して、お世話してくれる感じがしました。

たまにUbuntuに触るのも悪くないですね。

ではでは、また次回。