fjalvingh
fjalvingh

Reputation: 840

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 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:

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?

Upvotes: 8

Views: 6238

Answers (6)

Khoi Ngo
Khoi Ngo

Reputation: 1441

My quick way to disable that message: (macOS 12 Monterey M1 and above)

Open Terminal -> Preferences -> Advanced tab -> uncheck to Set locale environment variables on startup

Upvotes: 2

slhck
slhck

Reputation: 38652

In my case, I was SSHing into a server from my macOS machine, and I would run into this issue:

$ sudo su
# pg_upgradecluster 15 main
Stopping old cluster...
Restarting old cluster with restricted connections...
Notice: extra pg_ctl/postgres options given, bypassing systemctl for start operation
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
    LANGUAGE = (unset),
    LC_ALL = (unset),
    LC_CTYPE = "UTF-8",
    LC_TERMINAL = "iTerm2",
    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:
  LANG: en_US.UTF-8
  LC_CTYPE: UTF-8
  LC_TERMINAL: iTerm2
  LC_TERMINAL_VERSION: 3.4.23
Error: Could not create target cluster

The issue was, apparently, the locale forwarded from my machine. The fix is to run this, and not sudo su, before runnign the pg_upgradecluster command:

sudo su -

This will reset the locale for the root user.

Of course, you should make sure that the requested locale (en_US.UTF-8) is generated, via running:

sudo dpkg-reconfigure locales

and selecting the right one from there.

Upvotes: 2

user3136781
user3136781

Reputation: 91

Just came across this in fresh Ubuntu + PostgresQL install, after all those years.. either way, the solution is:

apt-get install locales

Upvotes: 0

insideClaw
insideClaw

Reputation: 488

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.

Upvotes: 5

Ramast
Ramast

Reputation: 7709

For me, I have followed many suggestions and still didn't work. The script mentioned

LC_TIME=en_UK but it's totally unrelated so I ignored it at first. Turns out this was the problem and doing "unset LC_TIME" was all I needed.

Posting here in case it happened to someone else.

Upvotes: 1

hiro protagonist
hiro protagonist

Reputation: 46849

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!

Upvotes: 6

Related Questions