Install/Change/Check locale for new or productive PostgreSQL Cluster
Introduction
Anyone who have ever installed a PostgreSQL Database Server ran into that issue. The locale is not correct. By default its "C" on OSX or "en_US.UTF-8" on most linux distributions.
You can do better and install PostgreSQL from scratch (the first time) with the correct locale or easily change the locale on your already productive cluster. Heres how you can do this.
New cluster installation
Example works for Debian and it's derivates (tested with Squeeze and Wheezy):
Execute the following statement. It first sets the desired locale and then installs PostgreSQL itself
export LC_ALL="de_DE.UTF-8" && apt-get install postgresql-9.1
Your fresh PG-Cluster will be installed with the locale you exported before. It's just magic!
Change locale from a running cluster
Quick `n Dirty Procedure
- Stop everything using the database
pg_dumpall > backup_today.sql
pg_dropcluster 9.1 main
pg_createcluster --locale de_DE.UTF-8 9.1 main
service postgresql start
psql -f backup_today.sql postgres
- Restart the services using the DB
Explained steps
- First make sure, all services using the DB are stopped (Webservices for ex.)
- Make a dump of the whole PG-Cluster with
pg_dumpall > backup_today.sql
. The easiest way is to use thepostgres
user, for ex.su - postgres
from root
2.1. If you want you can backup the old Database Files withmv /var/lib/postgresql/9.1 /var/lib/postgresql/9.1_old
- Back in
root
drop the old cluster config withpg_dropcluster 9.1 main
. Don't care if it complains about the missing cluster folder - you just moved it - Create the new Cluster config with the desired locale
pg_createcluster --locale de_DE.UTF-8 9.1 main
. For your convenience use old configuration names again (9.1 and main) - If everything went well, start the new cluster with
service postgresql start
- Go back into
postgres
user environment and restore all the data to the new cluster withpsql -f backup_today.sql postgres
- Restart all the stopped services from step 1 and check if everything still runs fine.
How can i check the locale?
- Log into
postgres
user bysu - postgres
from root - Run
psql
to get into the Database - Type and run
\l
there to list all your databases - In the list you can see the locale and encoding of all the databases. Also your old/migrated should have the new one