LoginSignup
17
17

More than 5 years have passed since last update.

Chef SoloでPostgreSQLのインストールと他ホストからの接続許可する

Last updated at Posted at 2014-01-04

PostgreSQLは、インストール直後は自ホストのみ接続許可されているため、他ホストから接続するためのレシピを残しておく。

実行環境

  • chef 11.8.2
  • CentOS 6.3

CookBookの作成

postgresという名前のcookbookを作る。

knife cookbook create postgres -o site-cookbooks

レシピ

「site-cookbooks/postgres/recipes/default.rb」に以下を記載する。

postgresqlのインストール

package "postgresql-server" do
     action :install
end

データベースの初期化

初期化コマンドを実行する。
コマンドを実行すると、「/var/lib/pgsql/data」ディレクトリが生成されるので、既に実行済みか否かはdataディレクトリの有無で判断する。

dataDir = "/var/lib/pgsql/data/"
if not File.exists? dataDir then
   execute "postgresql-init" do
      command "service postgresql initdb"
   end
end

設定ファイルの書き換え

外部からの接続許可のための設定ファイルを書き換える。

template "/var/lib/pgsql/data/pg_hba.conf" do
   mode 0600
end

template "/var/lib/pgsql/data/postgresql.conf" do
   mode 0600
end

設定ファイルの読み込みと自動起動設定

設定ファイルを書き換えたため、postgresqlを再起動する。
ポートを開放するため、iptablesは無効にしておく。

service "postgresql" do
    action [:enable, :restart]
end

service "iptables" do
   action [:disable, :stop]
end

テンプレート

postgresql.conf

「site-cookbooks/postgres/templates/default」へ「postgresql.conf.erb」として配置する。

他ホストからの接続を許可し、ポートを明示的に指定する。

postgresql.conf.erb
listen_addresses ='*'
port = 5432         # (change requires restart)

pg_hba.conf

「site-cookbooks/postgres/templates/default」へ「pg_hba.conf.erb」として配置する。
すべてのホストからの接続で、パスワード認証を利用する設定にしておく。

pg_hba.conf.erb
# IPv4 local connections:
host    all     all       0.0.0.0/0        password

以上。レシピとテンプレートは、以下のgithubへ配置。

17
17
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
17
17