2013-08-27 22:43:47 +02:00
# Define for creating a database. See README. md for more details.
define postgresql: : server: : database(
2015-01-09 04:08:23 +01:00
$c omment = undef,
2013-08-27 22:43:47 +02:00
$db name = $ title,
$ owner = $ postgresql: : server: : user,
$ tablespace = undef,
2013-11-14 16:57:00 +01:00
$ template = 'template0' ,
2013-08-27 22:43:47 +02:00
$e ncoding = $ postgresql: : server: : encoding,
$ locale = $ postgresql: : server: : locale,
$ istemplate = false
) {
$c reatedb_path = $ postgresql: : server: : createdb_path
$ user = $ postgresql: : server: : user
2013-09-19 17:30:15 +02:00
$ group = $ postgresql: : server: : group
2013-08-27 22:43:47 +02:00
$ psql_path = $ postgresql: : server: : psql_path
2014-04-17 21:09:07 +02:00
$ port = $ postgresql: : server: : port
2014-08-18 11:26:06 +02:00
$ version = $ postgresql: : server: : _version
2014-03-07 16:50:03 +01:00
$defa ult_db = $ postgresql: : server: : default_database
2013-08-27 22:43:47 +02:00
# Set the defaults for the postgresql_psql resource
Postgresql_psql {
psql_user = > $ user,
psql_group = > $ group,
psql_path = > $ psql_path,
2014-04-17 21:09:07 +02:00
port = > $ port,
2013-08-27 22:43:47 +02:00
}
# Optionally set the locale switch. Older versions of createdb may not accept
# - - locale, so if the parameter is undefined its safer not to pass it.
if ( $ version ! = '8.1' ) {
$ locale_option = $ locale ? {
undef = > '' ,
2013-09-19 22:15:09 +02:00
default = > "--locale=${locale} " ,
2013-08-27 22:43:47 +02:00
}
$ public_revoke_privilege = 'CONNECT'
} else {
$ locale_option = ''
$ public_revoke_privilege = 'ALL'
}
2013-09-19 22:15:09 +02:00
$e ncoding_option = $e ncoding ? {
undef = > '' ,
default = > "--encoding '${encoding}' " ,
}
2013-08-27 22:43:47 +02:00
2013-09-19 22:15:09 +02:00
$ tablespace_option = $ tablespace ? {
undef = > '' ,
default = > "--tablespace='${tablespace}' " ,
2013-08-27 22:43:47 +02:00
}
2014-04-17 21:09:07 +02:00
$c reatedb_command = "${createdb_path} --port='${port}' --owner='${owner}' --template=${template} ${encoding_option}${locale_option}${tablespace_option} '${dbname}'"
2013-09-19 22:15:09 +02:00
2013-08-27 22:43:47 +02:00
postgresql_psql { "Check for existence of db '${dbname} ' " :
command = > 'SELECT 1' ,
unless = > "SELECT datname FROM pg_database WHERE datname='${dbname}'" ,
2014-03-07 16:50:03 +01:00
db = > $defa ult_db,
2014-04-17 21:09:07 +02:00
port = > $ port,
2013-10-23 23:49:16 +02:00
require = > Class [ 'postgresql::server::service' ]
2013-08-27 22:43:47 +02:00
} ~ >
exec { $c reatedb_command :
refreshonly = > true ,
user = > $ user,
logoutput = > on_failure,
} ~ >
# This will prevent users from connecting to the database unless they' ve been
# granted privileges.
postgresql_psql {"REVOKE ${public_revoke_privilege} ON DATABASE \ "${dbname}\" FROM public " :
2014-03-07 16:50:03 +01:00
db = > $defa ult_db,
2014-04-17 21:09:07 +02:00
port = > $ port,
2013-08-27 22:43:47 +02:00
refreshonly = > true ,
}
2014-09-16 20:19:43 +02:00
Exec[ $c reatedb_command ] - >
2013-08-27 22:43:47 +02:00
postgresql_psql {"UPDATE pg_database SET datistemplate = ${istemplate} WHERE datname = '${dbname}' " :
unless = > "SELECT datname FROM pg_database WHERE datname = '${dbname}' AND datistemplate = ${istemplate}" ,
2014-03-07 16:50:03 +01:00
db = > $defa ult_db,
2013-08-27 22:43:47 +02:00
}
2015-01-09 04:08:23 +01:00
if $c omment {
Exec[ $c reatedb_command ] - >
postgresql_psql {"COMMENT ON DATABASE ${dbname} IS '${comment}' " :
unless = > "SELECT pg_catalog.shobj_description(d.oid, 'pg_database') as \" Description\ " FROM pg_catalog.pg_database d WHERE datname = '${dbname}' AND pg_catalog.shobj_description(d.oid, 'pg_database') = '${comment}'" ,
}
}
2013-08-27 22:43:47 +02:00
# Build up dependencies on tablespace
if( $ tablespace ! = undef and defined( Postgresql: : Server: : Tablespace[ $ tablespace] ) ) {
Postgresql: : Server: : Tablespace[ $ tablespace] - > Exec[ $c reatedb_command]
}
}