2 # For all details and documentation:
3 # http://github.com/inkling/puppet-postgresql
5 # Copyright 2012- Inkling Systems, Inc.
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 # http://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.
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.
22 define postgresql::database(
24 $owner = $postgresql::params::user,
26 $charset = $postgresql::params::charset,
27 $locale = $postgresql::params::locale,
30 include postgresql::params
32 # Set the defaults for the postgresql_psql resource
34 psql_user => $postgresql::params::user,
35 psql_group => $postgresql::params::group,
36 psql_path => $postgresql::params::psql_path,
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 ? {
44 default => "--locale=${locale}",
46 $public_revoke_privilege = 'CONNECT'
49 $public_revoke_privilege = 'ALL'
52 $createdb_command_tmp = "${postgresql::params::createdb_path} --owner='${owner}' --template=template0 --encoding '${charset}' ${locale_option} '${dbname}'"
54 if($tablespace == undef) {
55 $createdb_command = $createdb_command_tmp
58 $createdb_command = "${createdb_command_tmp} --tablespace='${tablespace}'"
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']
67 exec { $createdb_command :
69 user => $postgresql::params::user,
70 logoutput => on_failure,
73 # This will prevent users from connecting to the database unless they've been
75 postgresql_psql {"REVOKE ${public_revoke_privilege} ON DATABASE \"${dbname}\" FROM public":
76 db => $postgresql::params::user,
80 Exec [ $createdb_command ] ->
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}",