Fixing utf8 encoding issue in Postgres

I was trying to setup Postgres for rails on an Ubuntu machine. And on running

rake db:create

I ran into 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 "app_development" ENCODING = 'unicode'

The database.yml file before:

development:
  adapter: postgresql
  encoding: unicode
  database: app_development
  pool: 5
  username: amit
  password:
  template: template0

Where it will use template0 from the database.yml and will ask me to change the default encoding to something else.

The solution turned out to be changing the default Postgres template1 to use utf8 encoding by default. By following the following gist:

And changing database.yml to use template1 instead.

The database.yml file after:

development:
  adapter: postgresql
  encoding: unicode
  database: app_development
  pool: 5
  username: amit
  password:
  template: template1

I hope this helps someone who runs into the same issue as I did.

Comments