Add actual postgresl module from puppetlabs
[mirror/dsa-puppet.git] / 3rdparty / modules / postgresql / manifests / db.pp
1 # Define: postgresql::db
2 #
3 # This module creates database instances, a user, and grants that user
4 # privileges to the database.
5 #
6 # Since it requires class postgresql::server, we assume to run all commands as the
7 # postgresql user against the local postgresql server.
8 #
9 # TODO: support an array of privileges for "grant"; currently only supports a single
10 #  privilege, which is pretty useless unless that privilege is "ALL"
11 #
12 # Parameters:
13 #   [*title*]       - postgresql database name.
14 #   [*user*]        - username to create and grant access.
15 #   [*password*]    - user's password.  may be md5-encoded, in the format returned by the "postgresql_password"
16 #                            function in this module
17 #   [*charset*]     - database charset. defaults to 'utf8'
18 #   [*grant*]       - privilege to grant user. defaults to 'all'.
19 #   [*tablespace*]  - database tablespace. default to use the template database's tablespace.
20 #   [*locale*]      - locale for database. defaults to 'undef' (effectively 'C').
21 #
22 # Actions:
23 #
24 # Requires:
25 #
26 #   class postgresql::server
27 #
28 # Sample Usage:
29 #
30 #  postgresql::db { 'mydb':
31 #    user     => 'my_user',
32 #    password => 'password',
33 #    grant    => 'all'
34 #  }
35 #
36 define postgresql::db (
37   $user,
38   $password,
39   $charset    = $postgresql::params::charset,
40   $locale     = $postgresql::params::locale,
41   $grant      = 'ALL',
42   $tablespace = undef,
43   $istemplate = false
44 ) {
45   include postgresql::params
46
47   postgresql::database { $name:
48     # TODO: ensure is not yet supported
49     #ensure    => present,
50     charset    => $charset,
51     tablespace => $tablespace,
52     #provider  => 'postgresql',
53     require    => Class['postgresql::server'],
54     locale     => $locale,
55     istemplate => $istemplate,
56   }
57
58   if ! defined(Postgresql::Database_user[$user]) {
59     postgresql::database_user { $user:
60       # TODO: ensure is not yet supported
61       #ensure         => present,
62       password_hash   => $password,
63       #provider       => 'postgresql',
64       require         => Postgresql::Database[$name],
65     }
66   }
67
68   postgresql::database_grant { "GRANT ${user} - ${grant} - ${name}":
69     privilege       => $grant,
70     db              => $name,
71     role            => $user,
72     #provider       => 'postgresql',
73     require         => [Postgresql::Database[$name], Postgresql::Database_user[$user]],
74   }
75
76 }