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 include_recipe "munin"
22 include_recipe "prometheus"
25 package "postgresql-common"
27 node[:postgresql][:versions].each do |version|
28 package "postgresql-#{version}"
29 package "postgresql-client-#{version}"
30 package "postgresql-contrib-#{version}"
31 package "postgresql-server-dev-#{version}"
33 defaults = node[:postgresql][:settings][:defaults] || {}
34 settings = node[:postgresql][:settings][version] || {}
36 template "/etc/postgresql/#{version}/main/postgresql.conf" do
37 source "postgresql.conf.erb"
41 variables :version => version, :defaults => defaults, :settings => settings
42 notifies :reload, "service[postgresql]"
43 only_if { ::Dir.exist?("/etc/postgresql/#{version}/main") }
46 template "/etc/postgresql/#{version}/main/pg_hba.conf" do
47 source "pg_hba.conf.erb"
51 variables :early_rules => settings[:early_authentication_rules] || defaults[:early_authentication_rules],
52 :late_rules => settings[:late_authentication_rules] || defaults[:late_authentication_rules]
53 notifies :reload, "service[postgresql]"
54 only_if { ::Dir.exist?("/etc/postgresql/#{version}/main") }
57 template "/etc/postgresql/#{version}/main/pg_ident.conf" do
58 source "pg_ident.conf.erb"
62 variables :maps => settings[:user_name_maps] || defaults[:user_name_maps]
63 notifies :reload, "service[postgresql]"
64 only_if { ::Dir.exist?("/etc/postgresql/#{version}/main") }
67 link "/var/lib/postgresql/#{version}/main/server.crt" do
68 to "/etc/ssl/certs/ssl-cert-snakeoil.pem"
69 only_if { ::Dir.exist?("/var/lib/postgresql/#{version}/main") }
72 link "/var/lib/postgresql/#{version}/main/server.key" do
73 to "/etc/ssl/private/ssl-cert-snakeoil.key"
74 only_if { ::Dir.exist?("/var/lib/postgresql/#{version}/main") }
77 standby_mode = settings[:standby_mode] || defaults[:standby_mode]
78 primary_conninfo = settings[:primary_conninfo] || defaults[:primary_conninfo]
79 restore_command = settings[:restore_command] || defaults[:restore_command]
81 if restore_command || standby_mode == "on"
82 passwords = if primary_conninfo
83 data_bag_item(primary_conninfo[:passwords][:bag],
84 primary_conninfo[:passwords][:item])
87 template "/var/lib/postgresql/#{version}/main/recovery.conf" do
88 source "recovery.conf.erb"
92 variables :standby_mode => standby_mode,
93 :primary_conninfo => primary_conninfo,
94 :restore_command => restore_command,
95 :passwords => passwords
96 notifies :reload, "service[postgresql]"
97 only_if { ::Dir.exist?("/var/lib/postgresql/#{version}/main") }
100 template "/var/lib/postgresql/#{version}/main/recovery.conf" do
102 notifies :reload, "service[postgresql]"
103 only_if { ::Dir.exist?("/var/lib/postgresql/#{version}/main") }
108 service "postgresql" do
109 action [:enable, :start]
110 supports :status => true, :restart => true, :reload => true
113 ohai_plugin "postgresql" do
114 template "ohai.rb.erb"
118 package "libdbd-pg-perl"
120 clusters = node[:postgresql][:clusters] || []
122 clusters.each do |name, details|
123 suffix = name.tr("/", ":")
125 munin_plugin "postgres_bgwriter_#{suffix}" do
126 target "postgres_bgwriter"
128 conf_variables :port => details[:port]
131 munin_plugin "postgres_checkpoints_#{suffix}" do
132 target "postgres_checkpoints"
134 conf_variables :port => details[:port]
137 munin_plugin "postgres_connections_db_#{suffix}" do
138 target "postgres_connections_db"
140 conf_variables :port => details[:port]
143 munin_plugin "postgres_users_#{suffix}" do
144 target "postgres_users"
146 conf_variables :port => details[:port]
149 munin_plugin "postgres_xlog_#{suffix}" do
150 target "postgres_xlog"
152 conf_variables :port => details[:port]
155 next unless File.exist?("/var/lib/postgresql/#{details[:version]}/main/recovery.conf")
157 munin_plugin "postgres_replication_#{suffix}" do
158 target "postgres_replication"
160 conf_variables :port => details[:port]
164 uris = clusters.collect do |_, details|
165 "postgres@:#{details[:port]}/postgres?host=/run/postgresql"
168 template "/etc/prometheus/exporters/postgres_queries.yml" do
169 source "postgres_queries.yml.erb"
175 prometheus_exporter "postgres" do
178 options "--extend.query-path=/etc/prometheus/exporters/postgres_queries.yml"
179 environment "DATA_SOURCE_URI" => uris.sort.uniq.first,
180 "PG_EXPORTER_AUTO_DISCOVER_DATABASES" => "true",
181 "PG_EXPORTER_EXCLUDE_DATABASES" => "postgres,template0,template1"
182 subscribes :restart, "template[/etc/prometheus/exporters/postgres_queries.yml]"