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 package "postgresql-common"
22 node[:postgresql][:versions].each do |version|
23 package "postgresql-#{version}"
24 package "postgresql-client-#{version}"
25 package "postgresql-contrib-#{version}"
26 package "postgresql-server-dev-#{version}"
28 defaults = node[:postgresql][:settings][:defaults] || {}
29 settings = node[:postgresql][:settings][version] || {}
31 template "/etc/postgresql/#{version}/main/postgresql.conf" do
32 source "postgresql.conf.erb"
36 variables :version => version, :defaults => defaults, :settings => settings
37 notifies :reload, "service[postgresql]"
40 template "/etc/postgresql/#{version}/main/pg_hba.conf" do
41 source "pg_hba.conf.erb"
45 variables :early_rules => settings[:early_authentication_rules] || defaults[:early_authentication_rules],
46 :late_rules => settings[:late_authentication_rules] || defaults[:late_authentication_rules]
47 notifies :reload, "service[postgresql]"
50 template "/etc/postgresql/#{version}/main/pg_ident.conf" do
51 source "pg_ident.conf.erb"
55 variables :maps => settings[:user_name_maps] || defaults[:user_name_maps]
56 notifies :reload, "service[postgresql]"
59 link "/var/lib/postgresql/#{version}/main/server.crt" do
60 to "/etc/ssl/certs/ssl-cert-snakeoil.pem"
63 link "/var/lib/postgresql/#{version}/main/server.key" do
64 to "/etc/ssl/private/ssl-cert-snakeoil.key"
67 standby_mode = settings[:standby_mode] || defaults[:standby_mode]
68 primary_conninfo = settings[:primary_conninfo] || defaults[:primary_conninfo]
69 restore_command = settings[:restore_command] || defaults[:restore_command]
71 if restore_command || standby_mode == "on"
72 passwords = if primary_conninfo
73 data_bag_item(primary_conninfo[:passwords][:bag],
74 primary_conninfo[:passwords][:item])
77 template "/var/lib/postgresql/#{version}/main/recovery.conf" do
78 source "recovery.conf.erb"
82 variables :standby_mode => standby_mode,
83 :primary_conninfo => primary_conninfo,
84 :restore_command => restore_command,
85 :passwords => passwords
86 notifies :reload, "service[postgresql]"
89 template "/var/lib/postgresql/#{version}/main/recovery.conf" do
91 notifies :reload, "service[postgresql]"
96 service "postgresql" do
97 action [:enable, :start]
98 supports :status => true, :restart => true, :reload => true
101 ohai_plugin "postgresql" do
102 template "ohai.rb.erb"
106 package "libdbd-pg-perl"
108 clusters = node[:postgresql][:clusters] || []
110 clusters.each do |name, details|
111 suffix = name.tr("/", ":")
113 munin_plugin "postgres_bgwriter_#{suffix}" do
114 target "postgres_bgwriter"
116 conf_variables :port => details[:port]
119 munin_plugin "postgres_checkpoints_#{suffix}" do
120 target "postgres_checkpoints"
122 conf_variables :port => details[:port]
125 munin_plugin "postgres_connections_db_#{suffix}" do
126 target "postgres_connections_db"
128 conf_variables :port => details[:port]
131 munin_plugin "postgres_users_#{suffix}" do
132 target "postgres_users"
134 conf_variables :port => details[:port]
137 munin_plugin "postgres_xlog_#{suffix}" do
138 target "postgres_xlog"
140 conf_variables :port => details[:port]
143 next unless File.exist?("/var/lib/postgresql/#{details[:version]}/main/recovery.conf")
145 munin_plugin "postgres_replication_#{suffix}" do
146 target "postgres_replication"
148 conf_variables :port => details[:port]