Defined Type: postgresql::server::db

Defined in:
manifests/server/db.pp

Summary

Define for conveniently creating a role, database and assigning the correct permissions.

Overview

Parameters:

  • user (String[1])

    User to assign access to the database upon creation (will be created if not defined elsewhere). Mandatory.

  • password (Optional[Variant[String, Sensitive[String]]]) (defaults to: undef)

    Sets the password for the created user (if a user is created).

  • comment (Optional[String[1]]) (defaults to: undef)

    Defines a comment to be stored about the database using the PostgreSQL COMMENT command.

  • dbname (String[1]) (defaults to: $title)

    Sets the name of the database to be created.

  • encoding (Optional[String[1]]) (defaults to: $postgresql::server::encoding)

    Overrides the character set during creation of the database.

  • locale (Optional[String[1]]) (defaults to: $postgresql::server::locale)

    Overrides the locale during creation of the database.

  • grant (Variant[String[1], Array[String[1]]]) (defaults to: 'ALL')

    Specifies the permissions to grant during creation. Default value: ‘ALL’.

  • tablespace (Optional[String[1]]) (defaults to: undef)

    Defines the name of the tablespace to allocate the created database to.

  • template (String[1]) (defaults to: 'template0')

    Specifies the name of the template database from which to build this database. Defaults value: template0.

  • istemplate (Boolean) (defaults to: false)

    Specifies that the database is a template, if set to true.

  • owner (Optional[String[1]]) (defaults to: undef)

    Sets a user as the owner of the database.

  • port (Optional[Stdlib::Port]) (defaults to: undef)

    Specifies the port where the PostgreSQL server is listening on.

  • psql_user (String[1]) (defaults to: $postgresql::server::user)

    Overrides the default PostgreSQL super user and owner of PostgreSQL related files in the file system.

  • psql_group (String[1]) (defaults to: $postgresql::server::group)

    Overrides the default PostgreSQL user group to be used for related files in the file system.

  • instance (String[1]) (defaults to: 'main')

    The name of the Postgresql database instance.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'manifests/server/db.pp', line 18

define postgresql::server::db (
  String[1]                                    $user,
  Optional[Variant[String, Sensitive[String]]] $password   = undef,
  Optional[String[1]]                          $comment    = undef,
  String[1]                                    $dbname     = $title,
  Optional[String[1]]                          $encoding   = $postgresql::server::encoding,
  Optional[String[1]]                          $locale     = $postgresql::server::locale,
  Variant[String[1], Array[String[1]]]         $grant      = 'ALL',
  Optional[String[1]]                          $tablespace = undef,
  String[1]                                    $template   = 'template0',
  Boolean                                      $istemplate = false,
  Optional[String[1]]                          $owner      = undef,
  Optional[Stdlib::Port] $port = undef,
  String[1] $psql_user = $postgresql::server::user,
  String[1] $psql_group = $postgresql::server::group,
  String[1] $instance = 'main',
) {
  if ! defined(Postgresql::Server::Database[$dbname]) {
    postgresql::server::database { $dbname:
      comment    => $comment,
      encoding   => $encoding,
      tablespace => $tablespace,
      template   => $template,
      locale     => $locale,
      istemplate => $istemplate,
      owner      => $owner,
      port       => $port,
      user       => $psql_user,
      group      => $psql_group,
    }
  }

  if ! defined(Postgresql::Server::Role[$user]) {
    postgresql::server::role { $user:
      password_hash => $password,
      port          => $port,
      psql_user     => $psql_user,
      psql_group    => $psql_group,
      before        => Postgresql::Server::Database[$dbname],
    }
  }

  if ! defined(Postgresql::Server::Database_grant["GRANT ${user} - ${grant} - ${dbname}"]) {
    postgresql::server::database_grant { "GRANT ${user} - ${grant} - ${dbname}":
      privilege  => $grant,
      db         => $dbname,
      role       => $user,
      port       => $port,
      psql_user  => $psql_user,
      psql_group => $psql_group,
    } -> Postgresql_conn_validator<| db_name == $dbname |>
  }

  if ($tablespace != undef and defined(Postgresql::Server::Tablespace[$tablespace])) {
    Postgresql::Server::Tablespace[$tablespace] -> Postgresql::Server::Database[$name]
  }
}