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]"
45 template "/etc/postgresql/#{version}/main/pg_hba.conf" do
46 source "pg_hba.conf.erb"
50 variables :early_rules => settings[:early_authentication_rules] || defaults[:early_authentication_rules],
51 :late_rules => settings[:late_authentication_rules] || defaults[:late_authentication_rules]
52 notifies :reload, "service[postgresql]"
55 template "/etc/postgresql/#{version}/main/pg_ident.conf" do
56 source "pg_ident.conf.erb"
60 variables :maps => settings[:user_name_maps] || defaults[:user_name_maps]
61 notifies :reload, "service[postgresql]"
64 link "/var/lib/postgresql/#{version}/main/server.crt" do
65 to "/etc/ssl/certs/ssl-cert-snakeoil.pem"
68 link "/var/lib/postgresql/#{version}/main/server.key" do
69 to "/etc/ssl/private/ssl-cert-snakeoil.key"
72 standby_mode = settings[:standby_mode] || defaults[:standby_mode]
73 primary_conninfo = settings[:primary_conninfo] || defaults[:primary_conninfo]
74 restore_command = settings[:restore_command] || defaults[:restore_command]
76 if restore_command || standby_mode == "on"
77 passwords = if primary_conninfo
78 data_bag_item(primary_conninfo[:passwords][:bag],
79 primary_conninfo[:passwords][:item])
82 template "/var/lib/postgresql/#{version}/main/recovery.conf" do
83 source "recovery.conf.erb"
87 variables :standby_mode => standby_mode,
88 :primary_conninfo => primary_conninfo,
89 :restore_command => restore_command,
90 :passwords => passwords
91 notifies :reload, "service[postgresql]"
94 template "/var/lib/postgresql/#{version}/main/recovery.conf" do
96 notifies :reload, "service[postgresql]"
101 service "postgresql" do
102 action [:enable, :start]
103 supports :status => true, :restart => true, :reload => true
106 ohai_plugin "postgresql" do
107 template "ohai.rb.erb"
111 package "libdbd-pg-perl"
113 clusters = node[:postgresql][:clusters] || []
115 clusters.each do |name, details|
116 suffix = name.tr("/", ":")
118 munin_plugin "postgres_bgwriter_#{suffix}" do
119 target "postgres_bgwriter"
121 conf_variables :port => details[:port]
124 munin_plugin "postgres_checkpoints_#{suffix}" do
125 target "postgres_checkpoints"
127 conf_variables :port => details[:port]
130 munin_plugin "postgres_connections_db_#{suffix}" do
131 target "postgres_connections_db"
133 conf_variables :port => details[:port]
136 munin_plugin "postgres_users_#{suffix}" do
137 target "postgres_users"
139 conf_variables :port => details[:port]
142 munin_plugin "postgres_xlog_#{suffix}" do
143 target "postgres_xlog"
145 conf_variables :port => details[:port]
148 next unless File.exist?("/var/lib/postgresql/#{details[:version]}/main/recovery.conf")
150 munin_plugin "postgres_replication_#{suffix}" do
151 target "postgres_replication"
153 conf_variables :port => details[:port]
157 ports = clusters.collect do |_, details|
158 "port=#{details[:port]}"
161 file "/etc/prometheus/collectors/postgres_queries.yml" do
165 template "/etc/prometheus/exporters/postgres_queries.yml" do
166 source "postgres_queries.yml.erb"
172 prometheus_exporter "postgres" do
175 options "--extend.query-path=/etc/prometheus/exporters/postgres_queries.yml"
176 environment "DATA_SOURCE_NAME" => "user=postgres host=/run/postgresql #{ports.join(',')}",
177 "PG_EXPORTER_EXCLUDE_DATABASES" => "postgres,template0,template1"
178 subscribes :restart, "template[/etc/prometheus/exporters/postgres_queries.yml]"