I am trying to setup my rails app to use latin1 encoding using the following database.yml
development:
adapter: postgresql
database: pana_development
pool: 5
timeout: 5000
encoding: latin1
When running rake db:create
I get the following error:
Couldn't create database for {"adapter"=>"postgresql", "database"=>"pana_development", "pool"=>5, "timeout"=>5000, "encoding"=>"latin1"}
PG::Error: ERROR: encoding LATIN1 does not match locale en_US.UTF-8
DETAIL: The chosen LC_CTYPE setting requires encoding UTF8.
What do I need to do in order to setup rails using latin1 encoding in a postgresql database?
First, you need to supply the program versions.
Next, you need to understand that the default locale setting LC_CTYPE
is determined at cluster creation time. I quote the manual here:
Some locale categories must have their values fixed when the database is created. You can use different settings for different databases, but once a database is created, you cannot change them for that database anymore.
LC_COLLATE
andLC_CTYPE
are these categories. They affect the sort order of indexes, so they must be kept fixed, or indexes on text columns would become corrupt. (But you can alleviate this restriction using collations, as discussed in Section 22.2.) The default values for these categories are determined when initdb is run, and those values are used when new databases are created, unless specified otherwise in theCREATE DATABASE
command.
You can easily solve this with CREATE DATABASE
building on the template0
template database, which has no pre-determined objects in it.
CREATE DATABASE pana_development ENCODING 'LATIN1' TEMPLATE template0;
In Ruby, you can just supply a "template" parameter:
template: template0
Compare this closely related answer:
rake db:create encoding error with postgresql
For repeated use you should prepare a database like outlined above, put everything in it that you want in future databases and use this one as template. Any database can be used as template.
Or you create a new database cluster with matching LC_TYPE
and take it from there.