Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix "Error: the locale requested by the environment is invalid" during postgresql cluster upgrade (pg_upgradecluster)

After an upgrade from Ubuntu Server 14.04 to 16.04 I had to also upgrade my Postgres clusters from 9.3 to 9.5. The normal way to do that is to first drop the (empty) 9.5 cluster that the upgrade created:

# pg_dropcluster 9.5 main

and then to upgrade the old 9.3 cluster to 9.5:

# pg_upgradecluster 9.3 main

This however results in an error:

perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LANGUAGE = "en_US.UTF-8",
LC_ALL = (unset),
LC_PAPER = "nl_NL.UTF-8",
LC_ADDRESS = "nl_NL.UTF-8",
LC_MONETARY = "nl_NL.UTF-8",
LC_NUMERIC = "nl_NL.UTF-8",
LC_TELEPHONE = "nl_NL.UTF-8",
LC_IDENTIFICATION = "nl_NL.UTF-8",
LC_MEASUREMENT = "nl_NL.UTF-8",
LC_TIME = "nl_NL.UTF-8",
LC_NAME = "nl_NL.UTF-8",
LANG = "en_US.UTF-8"
are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_US.UTF-8").
Error: The locale requested by the environment is invalid.
Error: Could not create target cluster

This means I could not upgrade to Postgres 9.5.

I checked all locale settings:

  • the en_US.UTF-8 locale exists and is properly generated as checked with locale -a (it shows en_US.utf8 in its list)
  • The file /etc/environment contains LC_ALL=en_US.UTF-8 and LANG=en_US.UTF-8
  • /etc/default/locale contains the same setting for LANG, LANGUAGE and LC_ALL
  • I can start Perl without any issue using "perl -e exit"

The error message is generated from the pg_createcluster script which is called from pg_updatecluster. But running pg_createcluster from the command line works just fine, without any issue.

Workaround for the issue:

I used the following workaround to at least get the conversion to work. I edited the /usr/bin/pg_upgradecluster script, as follows:

  • Find the code where it calls pg_createcluster by looking for the comment "create new cluster"
  • That code consists of a series of "push" statements, ending in the suspicious line: delete $ENV{'LC_ALL'}
  • Notice that this LC_ALL is exactly the variable that is unset in the error message.
  • Comment out that delete comment by adding a '#' before it, then save.

This at least circumvents this problem and lets you run the upgrade.

My question: is this a bug in the pg_upgradecluster script, or is something else awry on my system?

like image 813
fjalvingh Avatar asked Nov 19 '16 11:11

fjalvingh


2 Answers

had the same problem on an ubuntu 16.04 server. what helped in my case was to generate all the locales that appear in your listing of $ locale:

$ sudo locale-gen "en_US.UTF-8"
$ sudo locale-gen "nl_NL.UTF-8"

good luck!

like image 135
hiro protagonist Avatar answered Nov 07 '22 02:11

hiro protagonist


In my case, it was complaining about

Error: The locale requested by the environment is invalid:
LANG: en_GB
LANGUAGE: en_GB:en

So I unset LANG and unset LANGUAGE and it worked.

like image 4
insideClaw Avatar answered Nov 07 '22 02:11

insideClaw