1 # PRIVATE CLASS: do not call directly
2 class postgresql::server::initdb {
3 $needs_initdb = $postgresql::server::needs_initdb
4 $initdb_path = $postgresql::server::initdb_path
5 $datadir = $postgresql::server::datadir
6 $xlogdir = $postgresql::server::xlogdir
7 $logdir = $postgresql::server::logdir
8 $encoding = $postgresql::server::encoding
9 $locale = $postgresql::server::locale
10 $data_checksums = $postgresql::server::data_checksums
11 $group = $postgresql::server::group
12 $user = $postgresql::server::user
13 $psql_path = $postgresql::server::psql_path
14 $port = $postgresql::server::port
15 $module_workdir = $postgresql::server::module_workdir
17 # Set the defaults for the postgresql_psql resource
21 psql_path => $psql_path,
23 cwd => $module_workdir,
26 if $::osfamily == 'RedHat' and $::selinux == true {
27 $seltype = 'postgresql_db_t'
28 $logdir_type = 'postgresql_log_t'
36 # Make sure the data directory exists, and has the correct permissions.
46 # Make sure the xlog directory exists, and has the correct permissions.
57 # Make sure the log directory exists, and has the correct permissions.
62 seltype => $logdir_type,
67 # Build up the initdb command.
69 # We optionally add the locale switch if specified. Older versions of the
70 # initdb command don't accept this switch. So if the user didn't pass the
71 # parameter, lets not pass the switch at all.
72 $ic_base = "${initdb_path} --encoding '${encoding}' --pgdata '${datadir}'"
73 $ic_xlog = $xlogdir ? {
75 default => "${ic_base} --xlogdir '${xlogdir}'"
78 # The xlogdir need to be present before initdb runs.
79 # If xlogdir is default it's created by package installer
81 $require_before_initdb = [$datadir, $xlogdir]
83 $require_before_initdb = [$datadir]
86 $ic_locale = $locale ? {
88 default => "${ic_xlog} --locale '${locale}'"
91 $initdb_command = $data_checksums ? {
94 default => "${ic_locale} --data-checksums"
97 # This runs the initdb command, we use the existance of the PG_VERSION
98 # file to ensure we don't keep running this command.
99 exec { 'postgresql_initdb':
100 command => $initdb_command,
101 creates => "${datadir}/PG_VERSION",
104 logoutput => on_failure,
105 require => File[$require_before_initdb],
106 cwd => $module_workdir,
108 # The package will take care of this for us the first time, but if we
109 # ever need to init a new db we need to copy these files explicitly
110 if $::operatingsystem == 'Debian' or $::operatingsystem == 'Ubuntu' {
111 if $::operatingsystemrelease =~ /^6/ or $::operatingsystemrelease =~ /^7/ or $::operatingsystemrelease =~ /^10\.04/ or $::operatingsystemrelease =~ /^12\.04/ {
114 path => "${datadir}/server.crt",
115 source => 'file:///etc/ssl/certs/ssl-cert-snakeoil.pem',
116 owner => $::postgresql::server::user,
117 group => $::postgresql::server::group,
119 require => Exec['postgresql_initdb'],
123 path => "${datadir}/server.key",
124 source => 'file:///etc/ssl/private/ssl-cert-snakeoil.key',
125 owner => $::postgresql::server::user,
126 group => $::postgresql::server::group,
128 require => Exec['postgresql_initdb'],
132 } elsif $encoding != undef {
134 # by default pg_createcluster encoding derived from locale
135 # but it do does not work by installing postgresql via puppet because puppet
136 # always override LANG to 'C'
137 postgresql_psql { "Set template1 encoding to ${encoding}":
138 command => "UPDATE pg_database
139 SET datistemplate = FALSE
140 WHERE datname = 'template1'
143 SET encoding = pg_char_to_encoding('${encoding}'), datistemplate = TRUE
144 WHERE datname = 'template1'",
145 unless => "SELECT datname FROM pg_database WHERE
146 datname = 'template1' AND encoding = pg_char_to_encoding('${encoding}')",