Defined Type: postgresql::dbcreate

Defined in:
manifests/dbcreate.pp

Overview

Quick implementation with database creation and default role grants Requires a db name ($name) and a role. To be adapted for specific cases

Parameters:

  • role (Any)
  • encoding (Any) (defaults to: 'SQL_ASCII')
  • locale (Any) (defaults to: 'C')
  • template (Any) (defaults to: '')
  • password (Any) (defaults to: '')
  • conntype (Any) (defaults to: 'host')
  • address (Any) (defaults to: '127.0.0.1/32')
  • auth_method (Any) (defaults to: 'md5')
  • auth_options (Any) (defaults to: '')


4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
# File 'manifests/dbcreate.pp', line 4

define postgresql::dbcreate (
  $role,
  $encoding     = 'SQL_ASCII',
  $locale       = 'C',
  $template     = '',
  $password     = '',
  $conntype     = 'host',
  $address      = '127.0.0.1/32',
  $auth_method  = 'md5',
  $auth_options = '') {
  include postgresql

  $real_template = $template ? {
    ''      => $postgresql::version ? {
      '9.3'   => 'template0',
      default => 'template1',
    },
    default => $template,
  }

  exec { "role_${name}":
    user    => $postgresql::process_user,
    path    => '/usr/bin:/bin:/usr/sbin:/sbin',
    unless  => "echo \\\\dg | psql | grep ${role} 2>/dev/null",
    command => "echo \"create role \\\"${role}\\\" nosuperuser nocreatedb nocreaterole noinherit nologin ; alter role \\\"${role}\\\" nosuperuser nocreatedb nocreaterole noinherit login encrypted password '${password}'; grant ${name} to \\\"${role}\\\";\" | /usr/bin/psql",
    require => [Service['postgresql']],
  } -> exec { "db_${name}":
    user    => $postgresql::process_user,
    path    => '/usr/bin:/bin:/usr/sbin:/sbin',
    unless  => "psql --list -t -A | grep -q \"^${name}|\"",
    command => "echo \"create database \\\"${name}\\\" with OWNER=\\\"${role}\\\" TEMPLATE=${real_template} ENCODING='${encoding}' LC_COLLATE='${locale}' LC_CTYPE='${locale}';\" | /usr/bin/psql",
    require => [Service['postgresql']];
  }

  postgresql::hba { "hba_${name}":
    ensure   => 'present',
    type     => $conntype,
    database => $name,
    user     => $role,
    address  => $address,
    method   => $auth_method,
    option   => $auth_options,
  }
}