database.pp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 = $postgresql::server::user,
  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. $change_ownership = false,
  13. ) {
  14. $createdb_path = $postgresql::server::createdb_path
  15. $user = $postgresql::server::user
  16. $group = $postgresql::server::group
  17. $psql_path = $postgresql::server::psql_path
  18. $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']
  23. } else {
  24. $version = $postgresql::server::_version
  25. }
  26. # If the connection settings do not contain a port, then use the local server port
  27. if $connect_settings != undef and has_key( $connect_settings, 'PGPORT') {
  28. $port = undef
  29. } else {
  30. $port = $postgresql::server::port
  31. }
  32. # Set the defaults for the postgresql_psql resource
  33. Postgresql_psql {
  34. psql_user => $user,
  35. psql_group => $group,
  36. psql_path => $psql_path,
  37. port => $port,
  38. connect_settings => $connect_settings,
  39. }
  40. if $change_ownership {
  41. # Change owner for existing database
  42. if !$owner {
  43. fail('Must specify an owner to change database ownership.')
  44. }
  45. postgresql_psql { "Change owner of db '${dbname}' to ${owner}":
  46. command => "ALTER DATABASE \"${dbname}\" OWNER TO ${owner}",
  47. onlyif => "SELECT datname FROM pg_database WHERE datname='${dbname}'",
  48. db => $default_db,
  49. require => Class['postgresql::server::service']
  50. }
  51. } else {
  52. # Create a new database
  53. # Optionally set the locale switch. Older versions of createdb may not accept
  54. # --locale, so if the parameter is undefined its safer not to pass it.
  55. if ($version != '8.1') {
  56. $locale_option = $locale ? {
  57. undef => '',
  58. default => "LC_COLLATE='${locale}' LC_CTYPE='${locale}'",
  59. }
  60. $public_revoke_privilege = 'CONNECT'
  61. } else {
  62. $locale_option = ''
  63. $public_revoke_privilege = 'ALL'
  64. }
  65. $template_option = $template ? {
  66. undef => '',
  67. default => "TEMPLATE=\"${template}\"",
  68. }
  69. $encoding_option = $encoding ? {
  70. undef => '',
  71. default => "ENCODING='${encoding}'",
  72. }
  73. $tablespace_option = $tablespace ? {
  74. undef => '',
  75. default => "TABLESPACE=\"${tablespace}\"",
  76. }
  77. if $createdb_path != undef{
  78. warning('Passing "createdb_path" to postgresql::database is deprecated, it can be removed safely for the same behaviour')
  79. }
  80. postgresql_psql { "Create db '${dbname}'":
  81. command => "CREATE DATABASE \"${dbname}\" WITH OWNER=\"${owner}\" ${template_option} ${encoding_option} ${locale_option} ${tablespace_option}",
  82. unless => "SELECT datname FROM pg_database WHERE datname='${dbname}'",
  83. db => $default_db,
  84. require => Class['postgresql::server::service']
  85. }~>
  86. # This will prevent users from connecting to the database unless they've been
  87. # granted privileges.
  88. postgresql_psql {"REVOKE ${public_revoke_privilege} ON DATABASE \"${dbname}\" FROM public":
  89. db => $default_db,
  90. refreshonly => true,
  91. }
  92. Postgresql_psql[ "Create db '${dbname}'" ]->
  93. postgresql_psql {"UPDATE pg_database SET datistemplate = ${istemplate} WHERE datname = '${dbname}'":
  94. unless => "SELECT datname FROM pg_database WHERE datname = '${dbname}' AND datistemplate = ${istemplate}",
  95. db => $default_db,
  96. }
  97. if $comment {
  98. # The shobj_description function was only introduced with 8.2
  99. $comment_information_function = $version ? {
  100. '8.1' => 'obj_description',
  101. default => 'shobj_description',
  102. }
  103. Postgresql_psql[ "Create db '${dbname}'" ]->
  104. postgresql_psql {"COMMENT ON DATABASE \"${dbname}\" IS '${comment}'":
  105. unless => "SELECT pg_catalog.${comment_information_function}(d.oid, 'pg_database') as \"Description\" FROM pg_catalog.pg_database d WHERE datname = '${dbname}' AND pg_catalog.${comment_information_function}(d.oid, 'pg_database') = '${comment}'",
  106. db => $dbname,
  107. }
  108. }
  109. # Build up dependencies on tablespace
  110. if($tablespace != undef and defined(Postgresql::Server::Tablespace[$tablespace])) {
  111. Postgresql::Server::Tablespace[$tablespace]->Postgresql_psql[ "Create db '${dbname}'" ]
  112. }
  113. }
  114. }