I am very new to postgresql. Lots of folks use it and swear by it. I have no complaints with it as a database and query engine. I struggle with the administrative aspects of it.
A rails project I’m working on uses postgresql and uses databases with UTF8 encoding. Well, if you’ve ever dived into what rake db:test:prepare does, you’ll know that along the chain it drops and recreates your databases. That, of cource, necessitates that your user have sufficient privileges to create databases. Straightforward stuff.
Well, today when trying to run my tests I was getting the generic error:
Couldn’t create database for {“adapter”=>”postgresql”, “database”=>”ha_ya_right”, “password”=>”nopes”, “pool”=>5, “timeout”=>5000, “username”=>”uh_uh”}
That was at the end of the reams of backtrace. The magic was at the top of those reams:
PG::Error: 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.
Yeah, templates. I missed that error and ended up using ruby-debug (best. gem. ever.) and breaking right in the postgresql connection adapter. I’ll have to remember to remove that line.
Anyway, my system is running Ubuntu 12.04, PostgreSQL 9.1.4, Ruby 1.9.2, and Rails 3.2.3. I had to get the database connection to use template0. It’s quite easy:
test: adapter: postgresql database: ha_ya_right password: nopes pool: 5 timeout: 5000 username: uh_uh template: template0
That last line is the money line. I mentioned all my environment specs, because some folks in the blogs I came across said that solution didn’t work for them. Hopefully this can save you some time.
HT: http://stackoverflow.com/questions/5821238/rake-dbcreate-encoding-error-with-postgresql