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?

有帮助吗?

解决方案 2

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 and LC_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 the CREATE 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.

其他提示

There is a script which I have found in Gist which worked like a charm

sudo su postgres

psql

update pg_database set datistemplate=false where datname='template1';
drop database Template1;
create database template1 with owner=postgres encoding='UTF-8'
  lc_collate='en_US.utf8' lc_ctype='en_US.utf8' template template0;

update pg_database set datistemplate=true where datname='template1';

You can get the original source here

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top