Solved: Encoding problems

Hallo,

I wanted to install a mastodon instance on an Ubuntu 16.04 server hosted by contabo.de. I’ve followed the official tutorial, but when I want to create the database I get the following error message:

mastodon@server:~/live$RAILS_ENV=production bundle exec rails db:setup
PG::InvalidParameterValue: ERROR:  encoding "UTF8" does not match locale "en_US"
DETAIL:  The chosen LC_CTYPE setting requires encoding "LATIN1".
: CREATE DATABASE "mastodon_production" ENCODING = 'unicode'
Couldn't create database for {"adapter"=>"postgresql", "pool"=>5, "timeout"=>5000, "encoding"=>"unicode", "database"=>"mastodon_production", "username"=>"mastodon", "password"=>nil, "host"=>"/var/run/postgresql", "port"=>5432, "prepared_statements"=>true}
rails aborted!
ActiveRecord::StatementInvalid: PG::InvalidParameterValue: ERROR:  encoding "UTF8" does not match locale "en_US"
DETAIL:  The chosen LC_CTYPE setting requires encoding "LATIN1".
: CREATE DATABASE "mastodon_production" ENCODING = 'unicode'
/home/mastodon/live/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.4/lib/active_record/connection_adapters/postgresql/database_statements.rb:73:in `async_exec'
....

Can someone help me?

Solved it: Gone back to a snapshot of my server before I started the installation.
Generated the en_US.UTF-8 locales:

sudo locale-gen en_US.UTF-8

edited the /etc/default/locale en_US -> en_US.UTF-8

and restarted the server. I’ve been able to setup the database afterwords.

1 Like

Try to change your server’s locale to en_US.UTF-8. If you don’t know how or can’t do it, a temporary command can help:

env LANG=en_US.UTF-8 RAILS_ENV=production bundle exec rails db:setup

and later prefixing all commands with env LANG=en_US.UTF-8

If it still complains, please provide the output of echo $LANG && env | grep ^LC_

I tried everything described on this page and I still cannot install mastodon:

mastodon@server:~/live$ echo $LANG && env | grep ^LC_
en_US.UTF-8
LC_ALL=en_US.UTF-8
mastodon@server:~/live$ env LANG=en_US.UTF-8 RAILS_ENV=production bundle exec rails db:setup
PG::InvalidParameterValue: ERROR:  encoding "UTF8" does not match locale "en_US"
DETAIL:  The chosen LC_CTYPE setting requires encoding "LATIN1".
: CREATE DATABASE "mastodon_production" ENCODING = 'unicode'
Couldn't create 'mastodon_production' database. Please check your configuration.
rails aborted!

Any idea of what can be wrong?

The solution for this problem is:

sudo 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';
\q

source: https://gist.github.com/joshteng/9895494