Will & Skill Developers

Will & Skill Developers


Thoughts, snippets and ideas from the team at Will & Skill AB, Stockholm.

Webdeveloper @ Will & Skill

Share


How to create a PostgreSQL database with Swedish encoding

Mohammed HammoudMohammed Hammoud

I just encountered a really annoying problem while trying to order a list alphabetically in Django. The items with the Swedish letters 'ÅÄÖ' was encoded to 'AAO' and therefore showed up incorrectly ordered.

After some serious investigation it all came down to PostgreSQL. Apparently we need to tell our Postgres database what kind of encoding we want to use.

If you're creating a new database from scratch you could just jump to step 3. But if you already have a database and you want to change the encoding of that database, just follow my lead:

1. Dump your database

PGPASSWORD="myDatabasePassword" pg_dump -U database_user database_name --format tar > ~/dbdump.tar  

2. Drop your database

DROP DATABASE database_name;  

3. Create a database with the correct encoding.

CREATE DATABASE database_name ENCODING 'UTF-8' LC_COLLATE 'sv_SE.UTF-8' LC_CTYPE 'sv_SE.UTF-8' TEMPLATE template0;  

4. Grant privileges on database to user

GRANT ALL PRIVILEGES ON DATABASE "database_name" to database_user;  

5. Grant privileges on database to user

GRANT ALL PRIVILEGES ON DATABASE "database_name" to database_user;  

6. Restore your data

pg_restore -U database_user -c -n public -d database_name dump.tar  

What we do at Will & Skill

At W&S we are avid users of PostgreSQL in many of our projects. We think it is a robust database with great performance and a lot of nice features that allows for a smooth developer experience! If you would like to know more about how we can help Your company utilise the full potential of PostgreSQL do not hesitate to get in touch with us at postgresql [at] willandskill [dot] se!

Webdeveloper @ Will & Skill

Comments