1 # Define for creating a database. See README.md for more details.
2 define postgresql::server::database(
7 $template = 'template0',
8 $encoding = $postgresql::server::encoding,
9 $locale = $postgresql::server::locale,
11 $connect_settings = $postgresql::server::default_connect_settings,
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
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']
24 $version = $postgresql::server::_version
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') {
31 $port = $postgresql::server::port
34 # Set the defaults for the postgresql_psql resource
39 psql_path => $psql_path,
41 connect_settings => $connect_settings,
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 ? {
49 default => "LC_COLLATE = '${locale}' LC_CTYPE = '${locale}'",
51 $public_revoke_privilege = 'CONNECT'
54 $public_revoke_privilege = 'ALL'
57 $template_option = $template ? {
59 default => "TEMPLATE = \"${template}\"",
62 $encoding_option = $encoding ? {
64 default => "ENCODING = '${encoding}'",
67 $tablespace_option = $tablespace ? {
69 default => "TABLESPACE = \"${tablespace}\"",
72 if $createdb_path != undef {
73 warning('Passing "createdb_path" to postgresql::database is deprecated, it can be removed safely for the same behaviour')
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']
82 # This will prevent users from connecting to the database unless they've been
84 ~> postgresql_psql { "REVOKE ${public_revoke_privilege} ON DATABASE \"${dbname}\" FROM public":
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}",
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',
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}'",
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}\""],
112 if defined(Postgresql::Server::Role[$owner]) {
113 Postgresql::Server::Role[$owner]->Postgresql_psql["ALTER DATABASE \"${dbname}\" OWNER TO \"${owner}\""]
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}\""],
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}\""]