View on GitHub

chrisgilmerproj

import chris; chris.blog()

Download this project as a .zip file Download this project as a tar.gz file

Creating a Postgres DB with UTF8 Encoding

09 Jul 2013

Today I had to figure out how to create a db in postgres with UTF8 encoding. My first attempt was to do this:

/usr/bin/sudo -u postgres /usr/bin/psql -e -c "CREATE USER vagrant WITH PASSWORD 'vagrant';"
/usr/bin/sudo -u postgres /usr/bin/psql -e -c "CREATE DATABASE vagrant OWNER vagrant ENCODING 'UTF-8';"

Of course I got this arcane message:

CREATE DATABASE vagrant OWNER vagrant ENCODING 'UTF-8';
ERROR:  encoding UTF8 does not match locale en_US
DETAIL:  The chosen LC_CTYPE setting requires encoding LATIN1.

I wasn’t quite sure what to do with this but some helpful googling got me here:

/usr/bin/sudo -u postgres /usr/bin/psql -e -c "CREATE USER vagrant WITH PASSWORD 'vagrant';"
/usr/bin/sudo -u postgres /usr/bin/psql -e -c "CREATE DATABASE vagrant OWNER vagrant ENCODING 'UTF-8' LC_COLLATE 'en_US.UTF-8' LC_CTYPE 'en_US.UTF-8' template template0;"

This worked perfectly for me. Hope this helps!

comments powered by Disqus