Configure Postgres for Docker to use a different default locale

If you are using Docker and want to have a Postgres container for your databases using the official Postgres image, you may want to change the default locale. As the docs explain you can extend the Debian-based images with a simple Dockerfile to set a different locale.

In my example I’ll use spanish (es_ES) as my new default locale.

This is my Dockerfile:

FROM postgres:latest
RUN localedef -i es_ES -c -f UTF-8 -A /usr/share/locale/locale.alias es_ES.UTF-8
ENV LANG es_ES.utf8

Then I build my new image and run a new container based on it

docker build -t postgres-es .
docker run --name postgresdb -e POSTGRES_PASSWORD=apassword -p 5432:5432 -d postgres-es

And now I can check that my default locale is spanish indeed

docker exec -it postgresdb psql -U postgres -l 

postgres=# \l
                               Listado de base de datos
  Nombre   |  DueƱo   | CodificaciĆ³n |  Collate   |   Ctype    |      Privilegios      
-----------+----------+--------------+------------+------------+-----------------------
 postgres  | postgres | UTF8         | es_ES.utf8 | es_ES.utf8 | 
 template0 | postgres | UTF8         | es_ES.utf8 | es_ES.utf8 | =c/postgres          +
           |          |              |            |            | postgres=CTc/postgres
 template1 | postgres | UTF8         | es_ES.utf8 | es_ES.utf8 | =c/postgres          +
           |          |              |            |            | postgres=CTc/postgres
(3 filas)

Hope you enjoyed it!