Script for dealing with creating Postgres databases that complain with:

PG::InvalidParameterValue: ERROR: encoding UTF8 does not match locale en_US
DETAIL: The chosen LC_CTYPE setting requires encoding LATIN1.

when trying to create the production DB with :

RAILS_ENV=production bundle exec rails db:setup

FIX :

 su  - postgres

psql

update pg_database set datistemplate=false where datname='template1';
drop database Template1;
create database template1 with owner=postgres encoding='UTF-8'
  lc_collate='en_US.utf8' lc_ctype='en_US.utf8' template template0;

update pg_database set datistemplate=true where datname='template1';

screen:

su - postgres
postgres@echosystem:~$ psql -U postgres
psql (9.4.9)
Type "help" for help.

postgres=# update pg_database set datistemplate=false where datname='template1';
UPDATE 1
postgres=# drop database Template1;
DROP DATABASE
postgres=# create database template1 with owner=postgres encoding='UTF-8'
postgres-# lc_collate='en_US.utf8' lc_ctype='en_US.utf8' template template0;
CREATE DATABASE
postgres=# update pg_database set datistemplate=true where datname='template1';
UPDATE 1
postgres=#

Enjoy !!

ref: https://gist.github.com/turboladen/6790847

by 🅴🆁🆁🅴🆄🆁32
Hosted on

🅴🅲🅷🔵🆂🆈🆂🆃🅴🅼