kuniku’s diary

はてなダイアリーから移行(旧 d.hatena.ne.jp/kuniku/)、表示がおかしな箇所はコメントをお願いします。記載されている内容は日付およびバージョンに注意してください。直近1年以上前は古い情報の可能性が高くなります。

postgresで複数のインスタンスをつくる

  • 一般的な利用(最も多く利用される場合)では1つのインスタンスに複数のDBを用意することが多いように思う。
  • インスタンスを複数設けるってのは、少ないのではないかと思う。
  • postgresではポート番号、データディレクトリを別にすれば複数インスタンスを作成可能。

やること

前提
postgresの
configure
make
make install
までは完了していることを前提とします。

ユーザとグループ

postgresをmakeしてる時点で、postgresグループは既に作成されていると思うんですが・・・

  • postgresグループを追加
    • groupadd
  • ユーザを追加
    • $ useradd -G postgres postgresdb1
  • postgresが使用するデータ(環境変数のPGDATAで設定する値)ディレクトリを決める
    • $ mkdir /var/postgres/postgresdb1

`` /var/postgres`` のディレクトリは postgresグループが write,changedirectoryできること

    • ls -laの結果
drwxrw-rw-   postgres postgres ・・・ postgres

chmod g+x ./postgres/
などでパーミッションを変更しておく

でないと、

initdb: could not access directory "/var/postgres/postgresdb1/data": 許可がありません

などとinitdbしたときにエラーとなります。


postgresdb1 ディレクトリとその配下については、postgresdb1所有者をpostgresdb1にしておく。

ユーザ環境変数

$su - postgresdb1
$cd ~/
vi ./bashrc
export PATH=$PATH:/usr/local/pgsql/bin
export PGDATA=/var/postgres/postgresdb1/data

新しいPostgreSQLのデータベースクラスタの作成

17.2. データベースクラスタの作成
http://www.sraoss.co.jp/PostgreSQL/Manual/document/pg837doc/html/creating-cluster.html
データベースクラスタとは、1つのサーバインスタンスで管理されるデータベースの集合です。

initdb
http://www.sraoss.co.jp/PostgreSQL/Manual/document/pg837doc/html/app-initdb.html

initdbはPostgreSQLのデータベースクラスタを新しく作成します。データベースクラスタとは、1つのサーバインスタンスで管理されるデータベースの集合
initdbは、サーバプロセスの所有者によって実行されなければなりません。 initdbによって作成されるファイルやディレクトリにサーバがアクセスする必要があるからです。サーバをrootとして実行することはできませんので、rootでinitdbを実行してはいけません

  • initdbする

postgresdb1 ユーザが作成するインスタンスの所有者とします。

$ su - postgresdb1
$ initdb --pgdata=/var/postgres/postgresdb1/data
    • pgdataを指定すれば、そこのディレクトリで初期化(initdb)します。

また、

 形式
$ initdb -D /usr/local/pgsql/data
 -Dオプションを使う代わりにPGDATA環境変数を設定することもできます
  • 実行ログ
[postgresdb1@localhost ~]$ initdb --pgdata=/var/postgres/postgresdb1/data
The files belonging to this database system will be owned by user "postgresdb1".
This user must also own the server process.

The database cluster will be initialized with locale ja_JP.UTF-8.
The default database encoding has accordingly been set to UTF8.
initdb: could not find suitable text search configuration for locale ja_JP.UTF-8
The default text search configuration will be set to "simple".

initdb: could not access directory "/var/postgres/postgresdb1/data": 許可がありません <<−−ここでパーミッションでエラー

ここからが実行したログサンプル

[postgresdb1@localhost ~]$ initdb --pgdata=/var/postgres/postgresdb1/data
The files belonging to this database system will be owned by user "pdb01".
This user must also own the server process.

The database cluster will be initialized with locale ja_JP.UTF-8.
The default database encoding has accordingly been set to UTF8.
initdb: could not find suitable text search configuration for locale ja_JP.UTF-8
The default text search configuration will be set to "simple".

creating directory /var/postgres/postgresdb1/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers/max_fsm_pages ... 32MB/204800
creating configuration files ... ok
creating template1 database in /var/postgres/postgresdb1/data/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
vacuuming database template1 ... ok
copying template1 to template0 ... ok
copying template1 to postgres ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the -A option the
next time you run initdb.

Success. You can now start the database server using:

    postgres -D /var/postgres/postgresdb1/data
or
    pg_ctl -D /var/postgres/postgresdb1/data -l logfile start
  • 結果サンプル

pgdataで指定した箇所にpostgresが使用する構成が出来上がります。

$ pwd
/var/postgres/postgresdb1/data
$ ls -la
-rw-------  1 postgresdb1    postgresdb1        4 10月 26 01:00 PG_VERSION
drwx------  5 postgresdb1    postgresdb1     4096 10月 26 01:00 base
drwx------  2 postgresdb1    postgresdb1     4096 10月 26 01:00 global
drwx------  2 postgresdb1    postgresdb1     4096 10月 26 01:00 pg_clog
-rw-------  1 postgresdb1    postgresdb1     3429 10月 26 01:00 pg_hba.conf
-rw-------  1 postgresdb1    postgresdb1     1460 10月 26 01:00 pg_ident.conf
drwx------  4 postgresdb1    postgresdb1     4096 10月 26 01:00 pg_multixact
drwx------  2 postgresdb1    postgresdb1     4096 10月 26 01:00 pg_subtrans
drwx------  2 postgresdb1    postgresdb1     4096 10月 26 01:00 pg_tblspc
drwx------  2 postgresdb1    postgresdb1     4096 10月 26 01:00 pg_twophase
drwx------  3 postgresdb1    postgresdb1     4096 10月 26 01:00 pg_xlog

DBの起動と利用

  • DBの利用するport番号を決める
    • ユーザ環境変数に PGPORT=54XX などと記述可能だけど postgressql.conf でポート番号は指定するほうがよいと思う。
  • デフォルトが5432なので、

5442、5452、5462・・・
5433、5434、5435・・・
と規則性をもった形でportを割りふるほうがよいと思う。
pgpoolを使うならば、それも考慮しておくほうがよい。

  • 実行ログの最後のほうの起動方法で起動してみる
$ su - postgresdb1
$ pg_ctl -D /var/postgres/postgresdb1/data start
  • DBをつくってみて、psql接続する
$ su - postgresdb1
$ createdb -p5432 abc
$ psql -p 5432 abc

Welcome to psql 8.3.8, the PostgreSQL interactive terminal.

Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help with psql commands
       \g or terminate with semicolon to execute query
       \q to quit

abc=# select version();
                                              version                                               
----------------------------------------------------------------------------------------------------
 PostgreSQL 8.3.8 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-46)
(1 row)

abc=# 

参考情報

http://www.tm-site.net/linuxroom/PostgreSQLmulti.htm