Ruby on rails 3 rake db:使用postgresql创建编码错误

Ruby on rails 3 rake db:使用postgresql创建编码错误,ruby-on-rails-3,postgresql,encoding,rake-task,Ruby On Rails 3,Postgresql,Encoding,Rake Task,我正在将一个正在工作的现有rails项目导入我的新arch linux系统,我已经正确安装了所有gems和postgresql,但在运行时遇到了一些问题: rake db:create 我得到以下错误 PGError: ERROR: new encoding (UTF8) is incompatible with the encoding of the template database (SQL_ASCII) HINT: Use the same encoding as in the t

我正在将一个正在工作的现有rails项目导入我的新arch linux系统,我已经正确安装了所有gems和postgresql,但在运行时遇到了一些问题:

rake db:create
我得到以下错误

PGError: ERROR:  new encoding (UTF8) is incompatible with the encoding of the template database (SQL_ASCII)
HINT:  Use the same encoding as in the template database, or use template0 as template.
: CREATE DATABASE "System_test" ENCODING = 'unicode'
我用正确的编码手动创建了数据库,迁移工作正常,但我可以运行

rake db:test:clone
命令,因为它尝试创建数据库,我也不想手动创建数据库。那么,有人知道如何解决这个问题吗

问候

编辑:这是我的数据库.yml

development:
  adapter: postgresql
  encoding: unicode
  database: System_development
  pool: 5
  username: forellana
  password:

test: &test
  adapter: postgresql
  encoding: unicode
  database: System_test
  pool: 5
  username: forellana
  password:

cucumber:
  <<: *test
开发:
适配器:postgresql
编码:unicode
数据库:系统开发
游泳池:5
用户名:forellana
密码:
测试:&测试
适配器:postgresql
编码:unicode
数据库:系统测试
游泳池:5
用户名:forellana
密码:
黄瓜:

这里的主要问题是,您的模板数据库(
template1
)是使用ASCII编码创建的,您告诉PostgreSQL使用UTF8编码创建新数据库。不用说,它对此并不特别满意。您可以做的是删除
模板1
数据库,然后使用重新创建它。当您的主机提供商没有正确设置区域设置时,这也可能是一个问题。你可以阅读更多关于


我在这篇关于

的文章中找到了所有这些信息。要在Rails中解决这个问题,我发现您只需在
数据库.yml的每个部分(开发/生产等)中添加以下行:

template: template0

有关其他选项,请参阅。

您可以按照发布的步骤将postgres template1更改为UTF


与其他答案类似,我发现这个要点非常有用。使用Ubuntu 14.04。我想将默认模板更改为使用UTF-8

进入postgres提示符:

Activate the postgres console.
su - postgres
psql
# First, we need to drop template1. Templates can’t be dropped, so we first modify it so t’s an ordinary database:

        UPDATE pg_database SET datistemplate = FALSE WHERE datname = 'template1';

# Now we can drop it:

        DROP DATABASE template1;

# Now its time to create database from template0, with a new default encoding:

        CREATE DATABASE template1 WITH TEMPLATE = template0 ENCODING = 'UNICODE';

# Now modify template1 so it’s actually a template:

        UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template1';

# Now switch to template1 and VACUUM FREEZE the template:

        \c template1

        VACUUM FREEZE;
然后键入以下命令:

Activate the postgres console.
su - postgres
psql
# First, we need to drop template1. Templates can’t be dropped, so we first modify it so t’s an ordinary database:

        UPDATE pg_database SET datistemplate = FALSE WHERE datname = 'template1';

# Now we can drop it:

        DROP DATABASE template1;

# Now its time to create database from template0, with a new default encoding:

        CREATE DATABASE template1 WITH TEMPLATE = template0 ENCODING = 'UNICODE';

# Now modify template1 so it’s actually a template:

        UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template1';

# Now switch to template1 and VACUUM FREEZE the template:

        \c template1

        VACUUM FREEZE;
这个问题应该得到解决。积分到:

添加

template: template0

在config/database.yml中为我工作:-)

你的database.yml文件是什么样子的?你是如何在你的系统上安装postgresql的?它是来源还是包装?
psql--command=“\l”
的输出是什么样子的?谢谢,我没有命令pg_dropcluster o,我不知道在哪里调用它们,但在那篇文章的第一条评论中,我找到了适合我的解决方案。非常好,我很高兴您找到了一个适合您的解决方案。如果您正在初始化数据库,请给我一个提示:
initdb-E UTF8
除非您确切知道自己在做什么,否则不要删除您的
template1
数据库。使用
template0
作为模板,或者创建一个新的DB集群,以您所需的设置开始。NO在rails 3.0.3中,postgresql 9.1.2在Ubuntu 10.04、postgresql 8.4.9和rails 3.1.3上运行。非常感谢。在Ubuntu 12.04、Rails 3.2.3、Ruby 1.9.2和PostgreSQL 9.1.4上工作。非常感谢。非常感谢。我不知道这个选项在Ubuntu12.04、Rails 3.2.12、Ruby 2.0.0和PostgreSQL 9.2上工作,捆绑在VM.OSX中,为我工作,用自制的pg安装,版本9.3.2、Rails 4.0.5错误:编码“UTF8”与区域设置“en_US”不匹配详细信息:选择的LC_CTYPE设置需要编码“LATIN1”。