rake db:使用 postgresql 创建编码错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5821238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
rake db:create encoding error with postgresql
提问by fespinozacast
I'm importing an existing rails project that I was working on into my new arch linux system, I already installed all gems and postgresql correctly, but I having some issues when runing:
我正在将我正在处理的现有 rails 项目导入到我的新 arch linux 系统中,我已经正确安装了所有 gems 和 postgresql,但是在运行时遇到了一些问题:
rake db:create
I get the following error
我收到以下错误
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'
I created manually the database with the correct encoding and the migrations worked well, but I can run the
我使用正确的编码手动创建了数据库并且迁移运行良好,但我可以运行
rake db:test:clone
command because It tries to create a database and I don't suppose to create manually the databases either. So, someone knows to fix this?
命令,因为它试图创建一个数据库,我也不认为手动创建数据库。那么,有人知道解决这个问题吗?
greetings
你好
EDIT: here's my database.yml
编辑:这是我的 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
and here's the complete output of the command
这是命令的完整输出
(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}
采纳答案by Jeremiah Peschka
The main problem here is that your template database (template1
) has been created with an ASCII encoding and you're telling PostgreSQL to create the new database with UTF8 encoding. Needless to say, it's not particularly pleased about that. What you can do is erase your template1
database and re-create it using these instructions. This can also be a problem when your hosting provider hasn't properly set the locale. You can read more about fixing your missing locales.
这里的主要问题是您的模板数据库 ( template1
) 是用 ASCII 编码创建的,而您告诉 PostgreSQL 用 UTF8 编码创建新数据库。不用说,它对此并不特别满意。您可以做的是擦除您的template1
数据库并使用这些说明重新创建它。当您的托管服务提供商未正确设置区域设置时,这也可能是一个问题。您可以阅读有关修复丢失的语言环境的更多信息。
I found all of this info through this post about Fixing PostgreSQL's default encoding on Ubuntu 9.10
我通过这篇关于在 Ubuntu 9.10 上修复 PostgreSQL 默认编码的帖子找到了所有这些信息
回答by S. Christoffer Eliesen
To fix this in Rails, I've found that you can simply add the following line to each section (development/production etc.) of your database.yml
file:
要在 Rails 中解决此问题,我发现您只需将以下行添加到文件的每个部分(开发/生产等)database.yml
:
template: template0
See ActiveRecord/ConnectionAdapters/PostgreSQL/SchemaStatements#create_databasefor other options.
有关其他选项,请参阅ActiveRecord/ConnectionAdapters/PostgreSQL/SchemaStatements#create_database。
回答by Ed Posnak
You can change postgres template1 to be UTF by doing the following as posted here:
您可以通过执行以下操作将 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';
回答by Hendrik
Similar to the other answers I found this gist to be very helpful. Using Ubuntu 14.04. I wanted to change the default template to use UTF-8.
与其他答案类似,我发现这个要点非常有帮助。使用 Ubuntu 14.04。我想更改默认模板以使用 UTF-8。
Get into the postgres prompt:
进入 postgres 提示符:
Activate the postgres console.
su - postgres
psql
Then type the following commands:
然后键入以下命令:
# 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;
Problem should be resolved. Credits to: https://gist.github.com/amolkhanorkar-webonise/8706915
问题应该得到解决。致谢:https: //gist.github.com/amolkhanorkar-webonise/8706915
回答by Saqib R.
Adding
添加
template: template0
in config/database.ymlworked for me :-)
在config/database.yml 中对我来说有效:-)