Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is going wrong with postgresql initdb? Why is the `UTF-8` encoding not getting enforced?

I am using PostgreSQL 9.1. Trying to enforce UTF8 encoding as default.

This is what I am doing.

service postgresql initdb -E 'UTF-8' --lc-collate='en_US.UTF-8' --lc-ctype=locale='en_US.UTF-8';

Although the initilization process goes on without any problem,

a \l at the psql prompt gives there details.

                         List of databases
   Name    |  Owner   |Encoding  | Collate | Ctype |   Access privileges   
-----------+----------+----------+---------+-------+-----------------------
  postgres | postgres | LATIN1 | en_US | en_US| 

Why is the UTF-8 encoding not getting enforced?

like image 997
ThinkingMonkey Avatar asked Aug 05 '12 07:08

ThinkingMonkey


People also ask

Does PostgreSQL support UTF-8?

The character set support in PostgreSQL allows you to store text in a variety of character sets (also called encodings), including single-byte character sets such as the ISO 8859 series and multiple-byte character sets such as EUC (Extended Unix Code), UTF-8, and Mule internal code.

What does Initdb do in PostgreSQL?

Description. initdb creates a new PostgreSQL database cluster. A database cluster is a collection of databases that are managed by a single server instance.

What encoding does PostgreSQL use?

ScaleGrid PostgreSQL deployments use UTF-8 as the default encoding on both client and server side. The template1 database is UTF-8 encoded, and uses the en_US. UTF-8 locale. By default any databases you create will also inherit this encoding.


2 Answers

Looks like you are calling initdb through a runlevel script of the OS. This script might not pass on the parameters. You better try executing initdb directly, you will need to perform the following steps starting as root and assuming the OS user account for the database is postgres.

mkdir <your data dir>
chown postgres <your data dir>
su postgres
initdb --pgdata=<your data dir> -E 'UTF-8' --lc-collate='en_US.UTF-8' --lc-ctype='en_US.UTF-8'
like image 190
Eelke Avatar answered Sep 28 '22 05:09

Eelke


Debian PostgreSQL installation automatically calls the initdb i.e. it initializes the cluster with default encoding and locale. Encoding can be changed later but the locale cannot. To change the locale (an possibly other options in initdb), delete the existing default cluster and create a new one:

Take root privileges.
Run the following command:



 pg_dropcluster --stop <version> main

For example:

pg_dropcluster --stop 8.3 main

Run the initdb with your options. For example:

pg_createcluster --locale de_DE.UTF-8 --start 8.3 main

Warning!

The previous operation obviously deletes everything you had in cluster databases. Perform this operation right after you have installed the base package. Check the PostgreSQL manual if you need to change locale for an existing database (it is not a trivial operation).

like image 35
jaydip Avatar answered Sep 28 '22 05:09

jaydip