45

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:

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

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}
2
  • What's your database.yml file look like? Commented Apr 28, 2011 at 15:50
  • How did you install postgresql on your system? Was it from source or from a package? What does the output of psql --command="\l" look like?
    – anon
    Commented Apr 28, 2011 at 16:09

5 Answers 5

80

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:

template: template0

See ActiveRecord/ConnectionAdapters/PostgreSQL/SchemaStatements#create_database for other options.

7
  • NO in rails 3.0.3 with postgresql 9.1.2
    – Juanin
    Commented Jan 25, 2012 at 12:33
  • Worked on Ubuntu 10.04, postgresql 8.4.9 and rails 3.1.3. Thank you!
    – Srisa
    Commented Feb 15, 2012 at 10:36
  • Worked on Ubuntu 12.04, Rails 3.2.3, Ruby 1.9.2, and PostgreSQL 9.1.4. THANK YOU!
    – juanpaco
    Commented Aug 23, 2012 at 13:45
  • Worked on Ubuntu 12.04, Rails 3.2.12, Ruby 2.0.0 and PostgreSQL 9.2, bundled in a VM. Commented Jun 1, 2013 at 10:31
  • Worked on Ubuntu 12.04, Rails 4.0.5, Ruby 2.1.1 and PostgreSQL 9.1.13 in a Vagrant VM. Thanks! Commented Jul 29, 2014 at 3:11
27

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.

I found all of this info through this post about Fixing PostgreSQL's default encoding on Ubuntu 9.10

4
  • Thanks, I don't have the commands pg_dropcluster o I didn't know where to invoke them, but on the first comment of that post I found the right solution for me. Commented Apr 28, 2011 at 16:31
  • 1
    Excellent, I'm glad that you found a solution that worked for you.
    – anon
    Commented Apr 28, 2011 at 18:52
  • 2
    Just a tip if you're initializing the database: initdb -E UTF8
    – l0b0
    Commented Nov 19, 2012 at 12:03
  • 4
    Don't delete your template1 database unless you know exactly what you are doing. Use template0 as template or create a new DB cluster with your desired settings to begin with. Commented Jul 2, 2013 at 16:09
22

You can change postgres template1 to be UTF by doing the following as posted 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';
2
  • OSX, worked for me, installed pg with homebrew, version 9.3.2, Rails 4.0.5
    – Valentin V
    Commented Jun 30, 2014 at 7:17
  • 1
    ERROR: encoding "UTF8" does not match locale "en_US" DETAIL: The chosen LC_CTYPE setting requires encoding "LATIN1".
    – Uilleann
    Commented Jan 13, 2017 at 16:35
6

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.

Get into the postgres prompt:

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

0
1

Adding

template: template0

in config/database.yml worked for me :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.