rake db:用postgresql创build编码错误

我正在将一个现有的rails项目导入到我的新的arch linux系统中,我已经正确安装了所有的gem和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' 

我用正确的编码方式手动创build了数据库,迁移效果很好,但我可以运行

 rake db:test:clone 

命令,因为它试图创build一个数据库,我不假设手动创build数据库。 那么,有人知道解决这个问题?

问候

编辑:这是我的database.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 

这里是命令的完整输出

 (in /home/fespinoza/Workspace/TLI) 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' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.5/lib/active_record/connection_adapters/abstract_adapter.rb:207:in `rescue in log' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.5/lib/active_record/connection_adapters/abstract_adapter.rb:199:in `log' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.5/lib/active_record/connection_adapters/postgresql_adapter.rb:493:in `execute' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.5/lib/active_record/connection_adapters/postgresql_adapter.rb:572:in `create_database' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.5/lib/active_record/railties/databases.rake:92:in `rescue in create_database' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.5/lib/active_record/railties/databases.rake:39:in `create_database' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.5/lib/active_record/railties/databases.rake:33:in `block (2 levels) in <top (required)>' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake.rb:636:in `call' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake.rb:636:in `block in execute' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake.rb:631:in `each' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake.rb:631:in `execute' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake.rb:597:in `block in invoke_with_call_chain' /home/fespinoza/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake.rb:583:in `invoke' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake.rb:2051:in `invoke_task' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake.rb:2029:in `block (2 levels) in top_level' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake.rb:2029:in `each' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake.rb:2029:in `block in top_level' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake.rb:2023:in `top_level' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake.rb:2001:in `block in run' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake.rb:1998:in `run' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/bin/rake:31:in `<top (required)>' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/bin/rake:19:in `load' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/bin/rake:19:in `<main>' Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "database"=>"System_test", "pool"=>5, "username"=>"forellana", "password"=>nil} 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_development" ENCODING = 'unicode' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.5/lib/active_record/connection_adapters/abstract_adapter.rb:207:in `rescue in log' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.5/lib/active_record/connection_adapters/abstract_adapter.rb:199:in `log' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.5/lib/active_record/connection_adapters/postgresql_adapter.rb:493:in `execute' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.5/lib/active_record/connection_adapters/postgresql_adapter.rb:572:in `create_database' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.5/lib/active_record/railties/databases.rake:92:in `rescue in create_database' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.5/lib/active_record/railties/databases.rake:39:in `create_database' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.5/lib/active_record/railties/databases.rake:35:in `block (2 levels) in <top (required)>' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake.rb:636:in `call' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake.rb:636:in `block in execute' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake.rb:631:in `each' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake.rb:631:in `execute' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake.rb:597:in `block in invoke_with_call_chain' /home/fespinoza/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake.rb:583:in `invoke' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake.rb:2051:in `invoke_task' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake.rb:2029:in `block (2 levels) in top_level' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake.rb:2029:in `each' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake.rb:2029:in `block in top_level' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake.rb:2023:in `top_level' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake.rb:2001:in `block in run' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake.rb:1998:in `run' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/bin/rake:31:in `<top (required)>' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/bin/rake:19:in `load' /home/fespinoza/.rvm/gems/ruby-1.9.2-p180/bin/rake:19:in `<main>' Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "database"=>"System_development", "pool"=>5, "username"=>"forellana", "password"=>nil} 

这里的主要问题是你的模板数据库( template1 )是用ASCII编码创build的,而你正在告诉PostgreSQL使用UTF8编码创build新的数据库。 不用说,这不是特别高兴。 你可以做的是删除你的template1数据库,并使用这些说明重新创build它。 当您的主机提供商没有正确设置区域时,这也可能是一个问题。 您可以阅读更多关于修复您的缺失语言环境 。

我通过这篇文章发现了所有关于在Ubuntu 9.10上修复PostgreSQL默认编码的信息

为了在Rails中解决这个问题,我发现你可以简单地将以下行添加到你的database.yml文件的每个部分(开发/生产等):

 template: template0 

有关其他选项,请参阅ConnectionAdapters :: PostgreSQLAdapter :: SchemaStatements#create_database 。

您可以通过执行以下操作将postgres template1更改为UTF :

 UPDATE pg_database SET datistemplate = FALSE WHERE datname = 'template1'; DROP DATABASE template1; CREATE DATABASE template1 WITH TEMPLATE = template0 ENCODING = 'UNICODE'; UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template1'; \c template1 VACUUM FREEZE; UPDATE pg_database SET datallowconn = FALSE WHERE datname = 'template1'; 

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

进入postgres提示符:

 Activate the postgres console. su - postgres psql 

然后input以下命令:

 # 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; 

问题应该解决。 致谢: https : //gist.github.com/amolkhanorkar-webonise/8706915

添加

 template: template0 

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