2 # Cookbook:: postgresql
5 # Copyright:: 2012, OpenStreetMap Foundation
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
11 # https://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
20 include_recipe "munin"
23 package "postgresql-common"
25 node[:postgresql][:versions].each do |version|
26 package "postgresql-#{version}"
27 package "postgresql-client-#{version}"
28 package "postgresql-contrib-#{version}"
29 package "postgresql-server-dev-#{version}"
31 defaults = node[:postgresql][:settings][:defaults] || {}
32 settings = node[:postgresql][:settings][version] || {}
34 template "/etc/postgresql/#{version}/main/postgresql.conf" do
35 source "postgresql.conf.erb"
39 variables :version => version, :defaults => defaults, :settings => settings
40 notifies :reload, "service[postgresql]"
43 template "/etc/postgresql/#{version}/main/pg_hba.conf" do
44 source "pg_hba.conf.erb"
48 variables :early_rules => settings[:early_authentication_rules] || defaults[:early_authentication_rules],
49 :late_rules => settings[:late_authentication_rules] || defaults[:late_authentication_rules]
50 notifies :reload, "service[postgresql]"
53 template "/etc/postgresql/#{version}/main/pg_ident.conf" do
54 source "pg_ident.conf.erb"
58 variables :maps => settings[:user_name_maps] || defaults[:user_name_maps]
59 notifies :reload, "service[postgresql]"
62 link "/var/lib/postgresql/#{version}/main/server.crt" do
63 to "/etc/ssl/certs/ssl-cert-snakeoil.pem"
66 link "/var/lib/postgresql/#{version}/main/server.key" do
67 to "/etc/ssl/private/ssl-cert-snakeoil.key"
70 standby_mode = settings[:standby_mode] || defaults[:standby_mode]
71 primary_conninfo = settings[:primary_conninfo] || defaults[:primary_conninfo]
72 restore_command = settings[:restore_command] || defaults[:restore_command]
74 if restore_command || standby_mode == "on"
75 passwords = if primary_conninfo
76 data_bag_item(primary_conninfo[:passwords][:bag],
77 primary_conninfo[:passwords][:item])
80 template "/var/lib/postgresql/#{version}/main/recovery.conf" do
81 source "recovery.conf.erb"
85 variables :standby_mode => standby_mode,
86 :primary_conninfo => primary_conninfo,
87 :restore_command => restore_command,
88 :passwords => passwords
89 notifies :reload, "service[postgresql]"
92 template "/var/lib/postgresql/#{version}/main/recovery.conf" do
94 notifies :reload, "service[postgresql]"
99 service "postgresql" do
100 action [:enable, :start]
101 supports :status => true, :restart => true, :reload => true
104 ohai_plugin "postgresql" do
105 template "ohai.rb.erb"
109 package "libdbd-pg-perl"
111 clusters = node[:postgresql][:clusters] || []
113 clusters.each do |name, details|
114 suffix = name.tr("/", ":")
116 munin_plugin "postgres_bgwriter_#{suffix}" do
117 target "postgres_bgwriter"
119 conf_variables :port => details[:port]
122 munin_plugin "postgres_checkpoints_#{suffix}" do
123 target "postgres_checkpoints"
125 conf_variables :port => details[:port]
128 munin_plugin "postgres_connections_db_#{suffix}" do
129 target "postgres_connections_db"
131 conf_variables :port => details[:port]
134 munin_plugin "postgres_users_#{suffix}" do
135 target "postgres_users"
137 conf_variables :port => details[:port]
140 munin_plugin "postgres_xlog_#{suffix}" do
141 target "postgres_xlog"
143 conf_variables :port => details[:port]
146 next unless File.exist?("/var/lib/postgresql/#{details[:version]}/main/recovery.conf")
148 munin_plugin "postgres_replication_#{suffix}" do
149 target "postgres_replication"
151 conf_variables :port => details[:port]