Add actual postgresl module from puppetlabs
[mirror/dsa-puppet.git] / 3rdparty / modules / postgresql / manifests / database.pp
1 # puppet-postgresql
2 # For all details and documentation:
3 # http://github.com/inkling/puppet-postgresql
4 #
5 # Copyright 2012- Inkling Systems, Inc.
6 #
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
10 #
11 #     http://www.apache.org/licenses/LICENSE-2.0
12 #
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.
18
19 # TODO: in order to match up more closely with the mysql module, this probably
20 #  needs to be moved over to ruby, and add support for ensurable.
21
22 define postgresql::database(
23   $dbname   = $title,
24   $owner = $postgresql::params::user,
25   $tablespace = undef,
26   $charset  = $postgresql::params::charset,
27   $locale   = $postgresql::params::locale,
28   $istemplate = false
29 ) {
30   include postgresql::params
31
32   # Set the defaults for the postgresql_psql resource
33   Postgresql_psql {
34     psql_user    => $postgresql::params::user,
35     psql_group   => $postgresql::params::group,
36     psql_path    => $postgresql::params::psql_path,
37   }
38
39   # Optionally set the locale switch. Older versions of createdb may not accept
40   # --locale, so if the parameter is undefined its safer not to pass it.
41   if ($postgresql::params::version != '8.1') {
42     $locale_option = $locale ? {
43       undef   => '',
44       default => "--locale=${locale}",
45     }
46     $public_revoke_privilege = 'CONNECT'
47   } else {
48     $locale_option = ''
49     $public_revoke_privilege = 'ALL'
50   }
51
52   $createdb_command_tmp = "${postgresql::params::createdb_path} --owner='${owner}' --template=template0 --encoding '${charset}' ${locale_option} '${dbname}'"
53
54   if($tablespace == undef) {
55     $createdb_command = $createdb_command_tmp
56   }
57   else {
58     $createdb_command = "${createdb_command_tmp} --tablespace='${tablespace}'"
59   }
60
61   postgresql_psql { "Check for existence of db '${dbname}'":
62     command => 'SELECT 1',
63     unless  => "SELECT datname FROM pg_database WHERE datname='${dbname}'",
64     require => Class['postgresql::server']
65   } ~>
66
67   exec { $createdb_command :
68     refreshonly => true,
69     user        => $postgresql::params::user,
70     logoutput   => on_failure,
71   } ~>
72
73   # This will prevent users from connecting to the database unless they've been
74   #  granted privileges.
75   postgresql_psql {"REVOKE ${public_revoke_privilege} ON DATABASE \"${dbname}\" FROM public":
76     db          => $postgresql::params::user,
77     refreshonly => true,
78   }
79
80   Exec [ $createdb_command ] ->
81
82   postgresql_psql {"UPDATE pg_database SET datistemplate = ${istemplate} WHERE datname = '${dbname}'":
83     unless => "SELECT datname FROM pg_database WHERE datname = '${dbname}' AND datistemplate = ${istemplate}",
84   }
85 }