Skip to content

Commit 16dbd14

Browse files
committed
Fix changing default encoding
Starting with 79fa355 it is not possible to set a custom encoding on systems where initdb is not needed (e.g. Debian). The autorequire introduce a circular dependency: ``` dependency cycles found: (Anchor[postgresql::server::service::begin] => Postgresql_psql[Set template1 encoding to UTF-8] => Class[Postgresql::Server::Initdb] => Postgresql_conf[listen_addresses] => Class[Postgresql::Server::Service] => Anchor[postgresql::server::service::begin]) ``` Move the relevant code from postgresql::server::initdb to postgresql::server::late_initdb and include it when applicable so that the postgresql_psql command gets run after the service has started up.
1 parent ecc63f1 commit 16dbd14

File tree

2 files changed

+40
-26
lines changed

2 files changed

+40
-26
lines changed

manifests/server/initdb.pp

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,8 @@
1313
$data_checksums = $postgresql::server::data_checksums
1414
$group = $postgresql::server::group
1515
$user = $postgresql::server::user
16-
$psql_path = $postgresql::server::psql_path
17-
$port = $postgresql::server::port
1816
$module_workdir = $postgresql::server::module_workdir
1917

20-
# Set the defaults for the postgresql_psql resource
21-
Postgresql_psql {
22-
psql_user => $user,
23-
psql_group => $group,
24-
psql_path => $psql_path,
25-
port => $port,
26-
cwd => $module_workdir,
27-
}
28-
2918
if $facts['os']['family'] == 'RedHat' and $facts['os']['selinux']['enabled'] == true {
3019
$seltype = 'postgresql_db_t'
3120
$logdir_type = 'postgresql_log_t'
@@ -147,20 +136,6 @@
147136
cwd => $module_workdir,
148137
}
149138
} elsif $encoding != undef {
150-
# [workaround]
151-
# by default pg_createcluster encoding derived from locale
152-
# but it do does not work by installing postgresql via puppet because puppet
153-
# always override LANG to 'C'
154-
postgresql_psql { "Set template1 encoding to ${encoding}":
155-
command => "UPDATE pg_database
156-
SET datistemplate = FALSE
157-
WHERE datname = 'template1'
158-
;
159-
UPDATE pg_database
160-
SET encoding = pg_char_to_encoding('${encoding}'), datistemplate = TRUE
161-
WHERE datname = 'template1'",
162-
unless => "SELECT datname FROM pg_database WHERE
163-
datname = 'template1' AND encoding = pg_char_to_encoding('${encoding}')",
164-
}
139+
include postgresql::server::late_initdb
165140
}
166141
}

manifests/server/late_initdb.pp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# @summary Manage the default encoding when database initialization is managed by the package
2+
#
3+
# @api private
4+
class postgresql::server::late_initdb {
5+
assert_private()
6+
7+
$encoding = $postgresql::server::encoding
8+
$user = $postgresql::server::user
9+
$group = $postgresql::server::group
10+
$psql_path = $postgresql::server::psql_path
11+
$port = $postgresql::server::port
12+
$module_workdir = $postgresql::server::module_workdir
13+
14+
# Set the defaults for the postgresql_psql resource
15+
Postgresql_psql {
16+
psql_user => $user,
17+
psql_group => $group,
18+
psql_path => $psql_path,
19+
port => $port,
20+
cwd => $module_workdir,
21+
}
22+
23+
# [workaround]
24+
# by default pg_createcluster encoding derived from locale
25+
# but it do does not work by installing postgresql via puppet because puppet
26+
# always override LANG to 'C'
27+
postgresql_psql { "Set template1 encoding to ${encoding}":
28+
command => "UPDATE pg_database
29+
SET datistemplate = FALSE
30+
WHERE datname = 'template1'
31+
;
32+
UPDATE pg_database
33+
SET encoding = pg_char_to_encoding('${encoding}'), datistemplate = TRUE
34+
WHERE datname = 'template1'",
35+
unless => "SELECT datname FROM pg_database WHERE
36+
datname = 'template1' AND encoding = pg_char_to_encoding('${encoding}')",
37+
before => Anchor['postgresql::server::service::end']
38+
}
39+
}

0 commit comments

Comments
 (0)