newer pg module
[mirror/dsa-puppet.git] / 3rdparty / modules / postgresql / manifests / server / database.pp
1 # Define for creating a database. See README.md for more details.
2 define postgresql::server::database(
3   $comment          = undef,
4   $dbname           = $title,
5   $owner            = undef,
6   $tablespace       = undef,
7   $template         = 'template0',
8   $encoding         = $postgresql::server::encoding,
9   $locale           = $postgresql::server::locale,
10   $istemplate       = false,
11   $connect_settings = $postgresql::server::default_connect_settings,
12 ) {
13   $createdb_path = $postgresql::server::createdb_path
14   $user          = $postgresql::server::user
15   $group         = $postgresql::server::group
16   $psql_path     = $postgresql::server::psql_path
17   $default_db    = $postgresql::server::default_database
18
19   # If possible use the version of the remote database, otherwise
20   # fallback to our local DB version
21   if $connect_settings != undef and has_key( $connect_settings, 'DBVERSION') {
22     $version = $connect_settings['DBVERSION']
23   } else {
24     $version = $postgresql::server::_version
25   }
26
27   # If the connection settings do not contain a port, then use the local server port
28   if $connect_settings != undef and has_key( $connect_settings, 'PGPORT') {
29     $port = undef
30   } else {
31     $port = $postgresql::server::port
32   }
33
34   # Set the defaults for the postgresql_psql resource
35   Postgresql_psql {
36     db               => $default_db,
37     psql_user        => $user,
38     psql_group       => $group,
39     psql_path        => $psql_path,
40     port             => $port,
41     connect_settings => $connect_settings,
42   }
43
44   # Optionally set the locale switch. Older versions of createdb may not accept
45   # --locale, so if the parameter is undefined its safer not to pass it.
46   if ($version != '8.1') {
47     $locale_option = $locale ? {
48       undef   => '',
49       default => "LC_COLLATE = '${locale}' LC_CTYPE = '${locale}'",
50     }
51     $public_revoke_privilege = 'CONNECT'
52   } else {
53     $locale_option = ''
54     $public_revoke_privilege = 'ALL'
55   }
56
57   $template_option = $template ? {
58     undef   => '',
59     default => "TEMPLATE = \"${template}\"",
60   }
61
62   $encoding_option = $encoding ? {
63     undef   => '',
64     default => "ENCODING = '${encoding}'",
65   }
66
67   $tablespace_option = $tablespace ? {
68     undef   => '',
69     default => "TABLESPACE = \"${tablespace}\"",
70   }
71
72   if $createdb_path != undef {
73     warning('Passing "createdb_path" to postgresql::database is deprecated, it can be removed safely for the same behaviour')
74   }
75
76   postgresql_psql { "CREATE DATABASE \"${dbname}\"":
77     command => "CREATE DATABASE \"${dbname}\" WITH ${template_option} ${encoding_option} ${locale_option} ${tablespace_option}",
78     unless  => "SELECT 1 FROM pg_database WHERE datname = '${dbname}'",
79     require => Class['postgresql::server::service']
80   }
81
82   # This will prevent users from connecting to the database unless they've been
83   #  granted privileges.
84   ~> postgresql_psql { "REVOKE ${public_revoke_privilege} ON DATABASE \"${dbname}\" FROM public":
85     refreshonly => true,
86   }
87
88   Postgresql_psql["CREATE DATABASE \"${dbname}\""]
89   -> postgresql_psql { "UPDATE pg_database SET datistemplate = ${istemplate} WHERE datname = '${dbname}'":
90     unless => "SELECT 1 FROM pg_database WHERE datname = '${dbname}' AND datistemplate = ${istemplate}",
91   }
92
93   if $comment {
94     # The shobj_description function was only introduced with 8.2
95     $comment_information_function =  $version ? {
96       '8.1'   => 'obj_description',
97       default => 'shobj_description',
98     }
99     Postgresql_psql["CREATE DATABASE \"${dbname}\""]
100     -> postgresql_psql { "COMMENT ON DATABASE \"${dbname}\" IS '${comment}'":
101       unless => "SELECT 1 FROM pg_catalog.pg_database d WHERE datname = '${dbname}' AND pg_catalog.${comment_information_function}(d.oid, 'pg_database') = '${comment}'",
102       db     => $dbname,
103     }
104   }
105
106   if $owner {
107     postgresql_psql { "ALTER DATABASE \"${dbname}\" OWNER TO \"${owner}\"":
108       unless  => "SELECT 1 FROM pg_database JOIN pg_roles rol ON datdba = rol.oid WHERE datname = '${dbname}' AND rolname = '${owner}'",
109       require => Postgresql_psql["CREATE DATABASE \"${dbname}\""],
110     }
111
112     if defined(Postgresql::Server::Role[$owner]) {
113       Postgresql::Server::Role[$owner]->Postgresql_psql["ALTER DATABASE \"${dbname}\" OWNER TO \"${owner}\""]
114     }
115   }
116
117   if $tablespace {
118     postgresql_psql { "ALTER DATABASE \"${dbname}\" SET ${tablespace_option}":
119       unless  => "SELECT 1 FROM pg_database JOIN pg_tablespace spc ON dattablespace = spc.oid WHERE datname = '${dbname}' AND spcname = '${tablespace}'",
120       require => Postgresql_psql["CREATE DATABASE \"${dbname}\""],
121     }
122
123     if defined(Postgresql::Server::Tablespace[$tablespace]) {
124       # The tablespace must be there, before we create the database.
125       Postgresql::Server::Tablespace[$tablespace]->Postgresql_psql["CREATE DATABASE \"${dbname}\""]
126     }
127   }
128 }