Chef で PostgreSQL を操作する
PostgreSQL の様な本格的なデータベース管理ソフトは、インストールした後の操作が厄介。
opscode のクックブック集にある database ( https://github.com/opscode/cookbooks/tree/master/database )は、PostgreSQL 以外にも対応した汎用的なDB操作を集めたクックブック。PostgreSQL のほかに MySQL や SQL Server に対応している。これを使えば、比較的に簡単にデータベースやユーザを作成してくれる。
bar というデータベースを作成するなら以下のようなレシピを作成すればよい。
include_recipe "build-essential" include_recipe "postgresql::server" include_recipe "database" postgresql_connection_info = {:host => "127.0.0.1", :port => 5432, :username => 'postgres', :password => node['postgresql']['password']['postgres']} database 'bar' do connection postgresql_connection_info provider Chef::Provider::Database::Postgresql action :create end
気づき:
- "postgresql::server" レシピ中で postgres ユーザのパスワードを自動生成され更新される。
- "database" クックブックでは、DBの操作に ruby の Pg ライブラリを利用している。
"postgresql::client" レシピ内で Pg ライブラリがインストールされているのがなぜか不明だったのだが、database クックブックで利用している訳だ。
と、そこまで書いた時点で、以下の Usage でちゃんと説明してあった。
https://github.com/opscode/cookbooks/tree/master/postgresql