<>initdb: invalid locale settings

experiment 1:

PostgreSQL Initialization is required after installation data, After compiling and installing with source code , adopt initdb During initialization :
initdb: invalid locale settings; check LANG and LC_* environment variables
Cannot be initialized .

experiment 2:

use rpm After package installation , adopt postgresql-11-setup initdb Time , Initialization was successful , But the default character set is SQL_ASCII:
postgres=# \l List of databases Name | Owner | Encoding | Collate | Ctype |
Access privileges
postgres| postgres | SQL_ASCII | C | C | template0 | postgres | SQL_ASCII | C |
C| =c/postgres + | | | | | postgres=CTc/postgres template0 | postgres |
SQL_ASCII| C | C | =c/postgres + | | | | | postgres=CTc/postgres
But check it out locale:
bash-4.2$ locale LANG=en_US.UTF-8 LC_CTYPE="en_US.UTF-8"
The system character set is OK , It's what you want en_US.UTF-8.

resolvent :

Through these two experiments, it is found that it is related to the character set ,mysql You can modify the default character set through a configuration file , however postgresql It can't be , Once initialized server_encoding It can't be modified :
postgres=# show server_encoding; server_encoding ----------------- SQL_ASCII (
1 row) postgres=# alter system set server_encoding = 'UTF8'; ERROR: parameter
"server_encoding" cannot be changed postgres=#

and template0 The character set of is also not modifiable , If SQL_ASCII In the case of template0 For template , Through the create Add after statement template=template0
encoding='UTF8’ To create UTF8 New library of character set . If it's not as much trouble , It can also be modified template1 The character set of is UTF8, Because it is not specified template, Will default to template1 For template .

Although the character set has been modified , But the symptoms are not the root cause .

experiment 1 Can not be initialized , For the use of initdb Direct initialization data The situation , Can be added -E 'UTF8’ To display the specified default character set to complete initialization .

For experiments 2, The command used is postgresql-11-setup
initdb, The command is not supported -E parameter , Or can't default on initialization UTF8, and locale The character set found is also en_US.UTF-8, no problem , In fact, the reason is that locales There is no corresponding language pack in :
bash-4.2$ locale -a C POSIX
This often occurs in docker in , Because a lot of things have been castrated for the sake of mirror image , There is no such container at present en_US.UTF-8 Language pack , Installation method :
yum remove glibc-common -y yum install glibc-common -y localedef -v -c -i
en_US -f UTF-8 en_US.UTF-8
Re execution locale -a You can find out more en_US.utf8, At this time, the new initdb, start-up , find :
postgres=# \l List of databases Name | Owner | Encoding | Collate | Ctype |
Access privileges
postgres| postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | template0 | postgres |
UTF8| en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres
template1| postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | |
postgres=CTc/postgres (3 rows)
If used dockerfile To create docker You can add environment variables to set the default character set :

©2019-2020 Toolsou All rights reserved,
The 11th Blue Bridge Cup python The real topic of the University Group National Games JavaSwing To achieve a simple Lianliankan games 【Spring Source code analysis 】42-@Conditional Detailed explanation element-ui Step on pit record 2019PHP Interview questions ( Continuously updated )PHPJava Misunderstanding —— Method overloading is a manifestation of polymorphism ? First issue 500 100 million , Set up a new Department , What is Tencent going to do ? Google chrome The browser can't open the web page , But what if other browsers can open it ? Regression of dependent variable order categories (R language )【Golang Basic series 10 】Go language On conditional sentences if