schema.pp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # = Type: postgresql::server::schema
  2. #
  3. # Create a new schema. See README.md for more details.
  4. #
  5. # == Requires:
  6. #
  7. # The database must exist and the PostgreSQL user should have enough privileges
  8. #
  9. # == Sample Usage:
  10. #
  11. # postgresql::server::schema {'private':
  12. # db => 'template1',
  13. # }
  14. #
  15. define postgresql::server::schema(
  16. $db = $postgresql::server::default_database,
  17. $owner = undef,
  18. $schema = $title,
  19. $connect_settings = $postgresql::server::default_connect_settings,
  20. $change_ownership = false,
  21. ) {
  22. $user = $postgresql::server::user
  23. $group = $postgresql::server::group
  24. $psql_path = $postgresql::server::psql_path
  25. $version = $postgresql::server::_version
  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. Postgresql_psql {
  33. db => $db,
  34. psql_user => $user,
  35. psql_group => $group,
  36. psql_path => $psql_path,
  37. port => $port,
  38. connect_settings => $connect_settings,
  39. }
  40. $schema_exists = "SELECT nspname FROM pg_namespace WHERE nspname='${schema}'"
  41. $authorization = $owner? {
  42. undef => '',
  43. default => "AUTHORIZATION \"${owner}\"",
  44. }
  45. if $change_ownership {
  46. # Change owner for existing schema
  47. if !$owner {
  48. fail('Must specify an owner to change schema ownership.')
  49. }
  50. $schema_title = "Change owner of schema '${schema}' to ${owner}"
  51. $schema_command = "ALTER SCHEMA \"${schema}\" OWNER TO ${owner}"
  52. postgresql_psql { $schema_title:
  53. command => $schema_command,
  54. onlyif => $schema_exists,
  55. require => Class['postgresql::server'],
  56. }
  57. } else {
  58. # Create a new schema
  59. $schema_title = "Create Schema '${title}'"
  60. $schema_command = "CREATE SCHEMA \"${schema}\" ${authorization}"
  61. postgresql_psql { $schema_title:
  62. command => $schema_command,
  63. unless => $schema_exists,
  64. require => Class['postgresql::server'],
  65. }
  66. }
  67. if($owner != undef and defined(Postgresql::Server::Role[$owner])) {
  68. Postgresql::Server::Role[$owner]->Postgresql_psql[$schema_title]
  69. }
  70. }