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.
21 package "postgresql-common"
23 node[:postgresql][:versions].each do |version|
24 package "postgresql-#{version}"
25 package "postgresql-client-#{version}"
26 package "postgresql-contrib-#{version}"
27 package "postgresql-server-dev-#{version}"
29 defaults = node[:postgresql][:settings][:defaults] || {}
30 settings = node[:postgresql][:settings][version] || {}
32 template "/etc/postgresql/#{version}/main/postgresql.conf" do
33 source "postgresql.conf.erb"
37 variables :version => version, :defaults => defaults, :settings => settings
38 notifies :reload, "service[postgresql]"
41 template "/etc/postgresql/#{version}/main/pg_hba.conf" do
42 source "pg_hba.conf.erb"
46 variables :early_rules => settings[:early_authentication_rules] || defaults[:early_authentication_rules],
47 :late_rules => settings[:late_authentication_rules] || defaults[:late_authentication_rules]
48 notifies :reload, "service[postgresql]"
51 template "/etc/postgresql/#{version}/main/pg_ident.conf" do
52 source "pg_ident.conf.erb"
56 variables :maps => settings[:user_name_maps] || defaults[:user_name_maps]
57 notifies :reload, "service[postgresql]"
60 link "/var/lib/postgresql/#{version}/main/server.crt" do
61 to "/etc/ssl/certs/ssl-cert-snakeoil.pem"
64 link "/var/lib/postgresql/#{version}/main/server.key" do
65 to "/etc/ssl/private/ssl-cert-snakeoil.key"
68 standby_mode = settings[:standby_mode] || defaults[:standby_mode]
69 primary_conninfo = settings[:primary_conninfo] || defaults[:primary_conninfo]
70 restore_command = settings[:restore_command] || defaults[:restore_command]
72 if restore_command || standby_mode == "on"
73 passwords = if primary_conninfo
74 data_bag_item(primary_conninfo[:passwords][:bag],
75 primary_conninfo[:passwords][:item])
78 template "/var/lib/postgresql/#{version}/main/recovery.conf" do
79 source "recovery.conf.erb"
83 variables :standby_mode => standby_mode,
84 :primary_conninfo => primary_conninfo,
85 :restore_command => restore_command,
86 :passwords => passwords
87 notifies :reload, "service[postgresql]"
90 template "/var/lib/postgresql/#{version}/main/recovery.conf" do
92 notifies :reload, "service[postgresql]"
97 service "postgresql" do
98 action [:enable, :start]
99 supports :status => true, :restart => true, :reload => true
102 ohai_plugin "postgresql" do
103 template "ohai.rb.erb"
107 package "libdbd-pg-perl"
109 clusters = node[:postgresql][:clusters] || []
111 clusters.each do |name, details|
112 suffix = name.tr("/", ":")
114 munin_plugin "postgres_bgwriter_#{suffix}" do
115 target "postgres_bgwriter"
117 conf_variables :port => details[:port]
120 munin_plugin "postgres_checkpoints_#{suffix}" do
121 target "postgres_checkpoints"
123 conf_variables :port => details[:port]
126 munin_plugin "postgres_connections_db_#{suffix}" do
127 target "postgres_connections_db"
129 conf_variables :port => details[:port]
132 munin_plugin "postgres_users_#{suffix}" do
133 target "postgres_users"
135 conf_variables :port => details[:port]
138 munin_plugin "postgres_xlog_#{suffix}" do
139 target "postgres_xlog"
141 conf_variables :port => details[:port]
144 next unless File.exist?("/var/lib/postgresql/#{details[:version]}/main/recovery.conf")
146 munin_plugin "postgres_replication_#{suffix}" do
147 target "postgres_replication"
149 conf_variables :port => details[:port]