CentOSでPostgreSQLのデータベースの格納場所を変更する

CentOSPostgreSQLをインストールしてデータベースの格納場所を変更するときにSELinuxではまったので、やったことのメモ。

環境

CentOS 6.4
PostgreSQL 9.3.4
SELinux Enforcing

データベース格納場所

デフォルト: /var/lib/pgsql
変更先: /home/pgsql

1. ディレクトリ作成

# mkdir /home/pgsql/data

2. 所有者、パーミッション変更

# chown -R postgres:postgres /home/pgsql
# chmod -R 700 /home/pgsql

3. PostgreSQLの設定ファイル編集

# vim /etc/sysconfig/pgsql/postgres
PGDATA=/home/pgsql/data
PGLOG=/home/pgsql/data/pgstartup.log

4. データベース初期化

# su postgres -c "initdb -D /home/pgsql/data"

5. SELinuxのコンテキスト設定

PostgreSQLのデータベースには postgresql_db_t ラベルが必要。

# semanage fcontext -a -t postgresql_db_t "/home/pgsql(/.*)?"
# restorecon -R /home/pgsql

6. SELinuxポリシー設定

postmaster(postgresqlの実行プロセス)が/homeにアクセスできるよう設定する。
(違う場所にデータベースを格納する場合も、/から格納場所までのすべてのディレクトリに以下のような設定が必要)

postmasterのドメインpostgresql_t
/homeのタイプは home_root_t

6.1. ポリシー・ソースファイル作成

適当な場所にpostgres_local.teを作成

# vim postgres_local.te

module postgresql_local 1.0;

require {
    type postgresql_t, home_root_t;
    class dir { search getattr };
}

allow postgresql_t home_root_t:dir { search getattr };
6.2. モジュールのコンパイル、インストール
# checkmodule -M -m -o postgres_local.mod postgres_local.te
# semodule_package -o postgres_local.pp -m postgres_local.mod
# semodule -i postgres_local.pp

7. 起動

# service postgresql start
Starting postgreSQL service:                 [  OK  ]

OKと出れば起動成功。



参考
11.4. 設定例