2011-04-28 171 views
45

我正在導入一個現有的rails項目,這是我正在使用的新的arch linux系統,我已經正確安裝了所有的gem和postgresql,但運行時遇到一些問題:rake db:用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 

命令,因爲它試圖創建一個數據庫,我不假設手動創建數據庫。那麼,有人知道要解決這個問題?

問候

編輯:這是我的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} 
+0

什麼是你的database.yml文件是什麼樣子? – 2011-04-28 15:50:55

+0

你是如何在你的系統上安裝postgresql的?來源還是包裝? 'psql --command =「\ l」'的輸出是什麼樣的? – 2011-04-28 16:09:19

回答

26

的完整輸出這裏的主要問題是,你的模板數據庫(template1)已經使用ASCII編碼創建,並告訴PostgreSQL使用UTF8編碼創建新的數據庫。不用說,這並不令人感到特別高興。你可以做的是擦除你的template1數據庫,並使用these instructions重新創建它。當您的主機提供商沒有正確設置區域設置時,這也可能是一個問題。你可以閱讀關於fixing your missing locales的更多信息。

我發現所有這些信息通過這篇文章瞭解Fixing PostgreSQL's default encoding on Ubuntu 9.10

+0

謝謝,我沒有命令pg_dropcluster o我不知道在哪裏調用它們,但是在那篇文章的第一條評論中,我找到了正確的解決方案。 – fespinozacast 2011-04-28 16:31:44

+1

非常好,我很高興你找到了適合你的解決方案。 – 2011-04-28 18:52:23

+2

只是一個提示,如果你正在初始化數據庫:'initdb -E UTF8' – l0b0 2012-11-19 12:03:24

81

要在Rails的解決這個問題,我發現,您可以在下面一行簡單地添加到每個部分(開發/生產等),您database.yml的文件:

template: template0 

請參閱ActiveRecord/ConnectionAdapters/PostgreSQL/SchemaStatements#create_database其他選項。

+0

沒有在rails 3.0.3中使用postgresql 9.1.2 – Juanin 2012-01-25 12:33:16

+0

曾在Ubuntu 10.04,postgresql 8.4.9和rails 3.1.3上工作。謝謝! – Srisa 2012-02-15 10:36:01

+0

曾在Ubuntu 12.04,Rails 3.2.3,Ruby 1.9.2和PostgreSQL 9.1.4上工作。 謝謝! – juanpaco 2012-08-23 13:45:26

21

可以更改Postgres的模板1通過做是UTF下面張貼here:

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'; 
+0

OSX,爲我工作,安裝pg with homebrew,版本9.3.2,Rails 4.0.5 – Valentin 2014-06-30 07:17:12

+0

這工作完美 – yeyo 2015-04-25 15:31:06

+0

錯誤:編碼「UTF8 「與語言環境不匹配」en_US「 細節:選擇的LC_CTYPE設置需要編碼」LATIN1「。 – Uilleann 2017-01-13 16:35:39

6

到我發現這個要點是非常有幫助的其他答案相似。使用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; 

問題應該得以解決。貸:https://gist.github.com/amolkhanorkar-webonise/8706915

+0

謝謝,這很好。 – 2015-02-25 14:42:16

1

配置添加

template: template0 

/database.yml的工作對我來說:-)