database.pp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # puppet-postgresql
  2. # For all details and documentation:
  3. # http://github.com/inkling/puppet-postgresql
  4. #
  5. # Copyright 2012- Inkling Systems, Inc.
  6. #
  7. # Licensed under the Apache License, Version 2.0 (the "License");
  8. # you may not use this file except in compliance with the License.
  9. # You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. # TODO: in order to match up more closely with the mysql module, this probably
  19. # needs to be moved over to ruby, and add support for ensurable.
  20. define postgresql::database(
  21. $dbname = $title,
  22. $tablespace = undef,
  23. $charset = $postgresql::params::charset,
  24. $locale = $postgresql::params::locale
  25. ) {
  26. include postgresql::params
  27. # Optionally set the locale switch. Older versions of createdb may not accept
  28. # --locale, so if the parameter is undefined its safer not to pass it.
  29. if ($postgresql::params::version != '8.1') {
  30. $locale_option = $locale ? {
  31. undef => '',
  32. default => "--locale=${locale}",
  33. }
  34. $public_revoke_privilege = 'CONNECT'
  35. } else {
  36. $locale_option = ''
  37. $public_revoke_privilege = 'ALL'
  38. }
  39. $createdb_command_tmp = "${postgresql::params::createdb_path} --template=template0 --encoding '${charset}' ${locale_option} '${dbname}'"
  40. if($tablespace == undef) {
  41. $createdb_command = $createdb_command_tmp
  42. }
  43. else {
  44. $createdb_command = "${createdb_command_tmp} --tablespace='${tablespace}'"
  45. }
  46. postgresql_psql { "Check for existence of db '${dbname}'":
  47. command => 'SELECT 1',
  48. unless => "SELECT datname FROM pg_database WHERE datname='${dbname}'",
  49. cwd => $postgresql::params::datadir,
  50. require => Class['postgresql::server']
  51. } ~>
  52. exec { $createdb_command :
  53. refreshonly => true,
  54. user => 'postgres',
  55. cwd => $postgresql::params::datadir,
  56. logoutput => on_failure,
  57. } ~>
  58. # This will prevent users from connecting to the database unless they've been
  59. # granted privileges.
  60. postgresql_psql {"REVOKE ${public_revoke_privilege} ON DATABASE ${dbname} FROM public":
  61. db => 'postgres',
  62. refreshonly => true,
  63. cwd => $postgresql::params::datadir,
  64. }
  65. }