3
11

More than 1 year has passed since last update.

【PostgreSQL】複数インスタンス作成

Last updated at Posted at 2020-07-17

1つのサーバに導入したPostgreSQLに複数ユーザでそれぞれインスタンス(データベースクラスタ)を作成したい場合の設定。
バージョンは10。

ポスグレはOS側ユーザが一つのインスタンスを持つので、まずインスタンスオーナーとなるOSユーザを作成します。
またグループもpostgresにしておきましょう。
(Postgresインストール時に作成されているはず)

ユーザー作成

adduser dangouser1


それぞれのユーザが持つデータベースディレクトリを作成

mkdir /database/data_dango1

場所は任意 

ディレクトリの所有を作成したユーザ、グループをpostgresに変更

※各ディレクトリには所有者のみがアクセスできるように、chmod700にしておく。

chown dangouser1 /database/data_dango1
chgrp postgres /database/data_dango1
chmod 700 /database/data_dango1


作成したユーザの環境変数を設定

su - dangousr1
$vi ./bashrc

以下を追記

./bashrc
export PATH=$PATH:/usr/local/pgsql/bin
export PGDATA=/database/data_dango1
export PATH
export PGDATA


$PATH:/usr/local/pgsql/bin
は共通なので(導入時に作成されるデフォルトインスタンスのディレクトリにコマンドが格納されてるので)、
/etc/profileで全体に通してもいいかもしれません。


設定反映

source bashrc


インスタンス作成

initdb --pgdata=/database/data_dango1

完了後、/database/data_dango1にインスタンスの構成データが作成されます。
こんな感じ

dangousr1@dangosvr:/database/data_dango1$ ls -lh

-rw------- 1 dangousr1 postgres 3 7月 30 23:50 PG_VERSION
drwxr-xr-x 2 dangousr1 postgres 12K 8月 27 23:50 archive_log
drwx------ 6 dangousr1 postgres 4.0K 7月 30 23:50 base
-rw------- 1 dangousr1 postgres 63 8月 28 00:00 current_logfiles
drwx------ 2 dangousr1 postgres 4.0K 8月 25 00:31 global
drwx------ 2 dangousr1 postgres 4.0K 7月 30 23:50 pg_commit_ts
drwx------ 2 dangousr1 postgres 4.0K 7月 30 23:50 pg_dynshmem
-rw------- 1 dangousr1 postgres 4.5K 7月 30 23:50 pg_hba.conf
-rw------- 1 dangousr1 postgres 1.6K 7月 30 23:50 pg_ident.conf
drwx------ 2 dangousr1 postgres 4.0K 8月 28 00:00 pg_log
drwx------ 4 dangousr1 postgres 4.0K 8月 27 23:55 pg_logical
drwx------ 4 dangousr1 postgres 4.0K 7月 30 23:50 pg_multixact
drwx------ 2 dangousr1 postgres 4.0K 8月 25 00:30 pg_notify
drwx------ 2 dangousr1 postgres 4.0K 8月 27 23:50 pg_replslot
drwx------ 2 dangousr1 postgres 4.0K 7月 30 23:50 pg_serial
drwx------ 2 dangousr1 postgres 4.0K 7月 30 23:50 pg_snapshots
drwx------ 2 dangousr1 postgres 4.0K 8月 25 00:30 pg_stat
drwx------ 2 dangousr1 postgres 4.0K 8月 28 11:51 pg_stat_tmp
drwx------ 2 dangousr1 postgres 4.0K 7月 31 23:50 pg_subtrans
drwx------ 2 dangousr1 postgres 4.0K 7月 30 23:50 pg_tblspc
drwx------ 2 dangousr1 postgres 4.0K 7月 30 23:50 pg_twophase

drwxr-xr-x 3 dangousr1 postgres 4096 8月 27 23:55 pg_wal
drwx------ 2 dangousr1 postgres 4.0K 7月 30 23:50 pg_xact
-rw------- 1 dangousr1 postgres 88 7月 30 23:50 postgresql.auto.conf
-rw------- 1 dangousr1 postgres 23K 7月 30 23:50 postgresql.conf
-rw------- 1 dangousr1 postgres 62 8月 25 00:30 postmaster.opts
-rw------- 1 dangousr1 postgres 80 8月 25 00:30 postmaster.pid
dangousr1@dangosvr:/database/data_dango1$ 

この中の、postgresql.confが設定ファイルなので編集します。

postgresql.conf
listen_addresses = '*'
port=5432以外に設定

ポートははじめは5432になっていますが、
デフォルトのインスタンスに使用されているので変更しましょう。

インスタンス起動

pg_ctl -D /database/data_dango1 start


デフォルトのDB(postrges)に接続

psql --port=5433 -d postgres

postgresql.confで設定したポート番号を指定します。

このようにポートで分けてユーザ、ディレクトリ、インスタンスを各々で用意してやれば複数インスタンスを作成できます。


いじょう。

3
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
11